Search code examples
windowsscriptingfile-rename

Script to rename files


I have about 2200 different files in a few different folders, and I need to rename about about 1/3 of them which are in their own subfolder. Those 700 are also in various folders as well.

For example, there might be The top-most folder is Employees, which has a few files in it, then the folder 2002 has a few, 2003 has more files, 2004 etc.

I just need to attach the word "Agreement" before the existing name of each file. So instead of it just being "Joe Schmoe.doc" It would be "Agreement Joe Schmoe.doc" instead.

I've tried googling such scripts, and I can find stuff similar to what I want but it all looks completely foreign to me so I can't understand how I'd modify it to suit my needs.

Oh, and this is for windows server '03.


Solution

  • I need about 2 minutes to write such script for *NIX systems (may be less), but for Windows it is a long song ... ))

    I've write simple VBS script for WSH, try it (save to {script-name}.vbs, change Path value (on the first line of the script) and execute). I recommend to test script on small amount of data for the first time just to be sure if it works correctly.

    Path = "C:\Users\rootDirectory"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Sub visitFolder(folderVar)
        For Each fileToRename In folderVar.Files
            fileToRename.Name = "Agreement " & fileToRename.Name
        Next
        For Each folderToVisit In folderVar.SubFolders
            visitFolder(folderToVisit)
        Next
    End Sub
    
    If FSO.FolderExists(Path) Then
        visitFolder(FSO.getFolder(Path))
    End If