none
Vbscript to rename files and replace special characters RRS feed

  • Question

  • Dear Exprt,

    would you please help to add addtional requirement for rename and replace special characters for file

    by the below script i can re name.


    strAnswer = InputBox("Please enter folder location to rename files:", _
        "File rename")
        strfilenm = InputBox("Enter name:", _
        "Rename Files")

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Sub visitFolder(folderVar)
        ForEach fileToRenameIn folderVar.Files
            fileToRename.Name = strfilenm & fileToRename.Name
        Next
        ForEach folderToVisitIn folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    EndSub

    If FSO.FolderExists(strAnswer) Then
        visitFolder(FSO.getFolder(strAnswer))
    EndIf



    Support@Mytechnet.me

    Wednesday, April 30, 2014 6:10 AM

Answers

  • Please spend time learning the basics of scripting.  We have tried to show you this many, many times.  You still are missing the simple things. 

    You can't just copy and paste things together with no understanding of the scripts you are pasting.

    Take the time to learn script then we will be able to help you.


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 7:10 AM

All replies

  • What is your question?  What errors are you getting?


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 6:15 AM
  • i am trying to replace special character from file name

    example : if the file name have \ : * ? " < > | # <TAB> { } % ~
    &
    , replace with some other character


    Support@Mytechnet.me

    Wednesday, April 30, 2014 6:19 AM
  • You need to use the Replace function.

    You are using a script that renames files.  It won't help you.

    Start here: http://technet.microsoft.com/library/ee176977.aspx


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 6:44 AM
  • I have try by this way al not help


    strAnswer = InputBox("Please enter folder location to rename files:", _
        "test")
        strfilenm = InputBox("Enter name:", _
        "Rename Files")

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Sub visitFolder(folderVar)
        ForEach fileToRenameIn folderVar.Files
            fileToRename.Name = strfilenm & fileToRename.Name & strClean
        Next
        ForEach folderToVisitIn folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    EndSub

    If FSO.FolderExists(strAnswer) Then
        visitFolder(FSO.getFolder(strAnswer))
    EndIf


    Function strClean (strtoclean)
    tempstr = strtoclean
    charArray = Array("?", "/", "\", ":", "*", """", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".")

    ForEach tmpCharin charArray

    SelectCase tmpChar
    Case"&"
    changeTo = " and "
    Case":"
    changeTo = "-"
    CaseElse
    changeTo = " "
    EndSelect

    tempstr = replace( tempstr, tmpChar, changeTo )
    Next

    strClean = tempstr
    EndFunction



    Support@Mytechnet.me


    Wednesday, April 30, 2014 6:53 AM
  • Please spend time learning the basics of scripting.  We have tried to show you this many, many times.  You still are missing the simple things. 

    You can't just copy and paste things together with no understanding of the scripts you are pasting.

    Take the time to learn script then we will be able to help you.


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 7:10 AM
  • Here.  Study this on how to use subroutines: http://technet.microsoft.com/en-us/library/ee176984.aspx


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 7:12 AM
  • Thx would you please look below what wrong in its run  nothing happend no error

    strAnswer = InputBox("Please enter folder location to rename files:", _
        "Test")
        strfilenm = InputBox("Enter name:", _
        "Rename Files")

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set regEx = NewRegExp

    'Your pattern here
    SelectCase tmpChar
    Case"&"
    changeTo = " and "
    Case"/"
    changeTo = "_"
    CaseElse
    changeTo = " "
    EndSelect
    regEx.Pattern = tmpChar 

    Sub visitFolder(folderVar)
        ForEach fileToRename In folderVar.Files
            fileToRename.Name = strfilenm & fileToRename.Name 
            fileToRename.Name = regEx.Replace(fileToRename.Name, tmpChar)
            
        Next
        ForEach folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    EndSub


    Support@Mytechnet.me

    Wednesday, April 30, 2014 7:19 AM
  • Please read and study the link I posted. 


    ¯\_(ツ)_/¯

    Wednesday, April 30, 2014 7:24 AM
  • Thx i will

    Support@Mytechnet.me

    Wednesday, April 30, 2014 7:25 AM
  • Dears,

    I receive below error for following script


    OptionExplicit
    Dim objFSO, objStartFolder, objFolder, regEx, speChar, changeTo, colFiles, objFile
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Your folder here
    objStartFolder = "c:\temp\test\"

    Set objFolder = objFSO.GetFolder(objStartFolder)
    Set regEx = NewRegExp

    'Your pattern here
    'regEx.Pattern = "[@]"

    regEx.Pattern = speChar

    SelectCase speChar
    Case"[&]"
    changeTo = "and"
    Case"[/]"
    changeTo = "_"
    CaseElse
    changeTo = " "
    EndSelect

    Set colFiles = objFolder.Files
    ForEach objFilein colFiles
        objFile.Name = regEx.Replace(objFile.Name, speChar)
    Next





    Support@Mytechnet.me

    Wednesday, April 30, 2014 8:09 AM