Windows – Bulk rename files to name of parent folder

batch-renamewindowswindows server 2012

I have a large (aprox. 150,000) tiff files which all have the same filename. They are unique because of the directory structure they are held in.

I would like to bulk rename the tiff files so they become unique, based on the directory structre that they are held within.

Does anyone have any method of acheiving this?

I am using Windows Server 2012 so a solution using a cmd script, batch file or windows GUI tool would be perfect.

Ideally, this is what I would like to acheieve, but if I have to have more or all of the directory structure in the final filename thsi would still be very, very helpful.

C:\A_001\B_0001\ABC\0001.tif -> ABC.tif

C:\A_001\B_0001\JKL:\0001.tif -> JKL.tif

C:\A_001\B_0001\XYZ\0001.tif -> XYZ.tif

C:\A_001\B_0002\123\0001.tif -> 123.tif

C:\A_001\B_0002\456\0001.tif -> 456.tif

C:\A_001\B_0002\789\0001.tif -> 789.tif

Best Answer

You can easy do it with VBScript like this (not tested!):

Const cRootDir = "here_to_start"
Const sFileToRename = "here_to_rename"
Set Fso = CreateObject("Scripting.FileSystemObject")
Dim oRootDir : Set oRootDir = Fso.GetFolder(cRootDir)

For Each subfolder In oRootDir
   For Each oFile in subfolder.Files
     If oFile.Name = sFileToRename  Then Fso.MoveFile oFile, subfolder & ".tif"
   Next
Next

You can start this script with

cscript.exe myScript.vbs

Related Question