How do I create batch file to rename large number of files in a folder? How do I create batch file to rename large number of files in a folder? windows windows

How do I create batch file to rename large number of files in a folder?


@echo offSETLOCAL ENABLEDELAYEDEXPANSIONSET old=Vacation2010SET new=Decemberfor /f "tokens=*" %%f in ('dir /b *.jpg') do (  SET newname=%%f  SET newname=!newname:%old%=%new%!  move "%%f" "!newname!")

What this does is it loops over all .jpg files in the folder where the batch file is located and replaces the Vacation2010 with December inside the filenames.


dir /b *.jpg >file.bat

This will give you lines such as:

Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg

Edit file.bat in your favorite Windows text-editor, doing the equivalent of:

s/Vacation2010(.+)/rename "&" "December \1"/

That's a regex; many editors support them, but none that come default with Windows (as far as I know). You can also get a command line tool such as sed or perl which can take the exact syntax I have above, after escaping for the command line.

The resulting lines will look like:

rename "Vacation2010 001.jpg" "December 001.jpg"
rename "Vacation2010 002.jpg" "December 002.jpg"
rename "Vacation2010 003.jpg" "December 003.jpg"

You may recognize these lines as rename commands, one per file from the original listing. ;) Run that batch file in cmd.exe.


you can do this easily without manual editing or using fancy text editors. Here's a vbscript.

Set objFS = CreateObject("Scripting.FileSystemObject")strFolder="c:\test"Set objFolder = objFS.GetFolder(strFolder)For Each strFile In objFolder.Files    If objFS.GetExtensionName(strFile) = "jpg" Then            strFileName = strFile.Name        If InStr(strFileName,"Vacation2010") > 0 Then                       strNewFileName = Replace(strFileName,"Vacation2010","December")            strFile.Name = strNewFileName        End If     End If  Next 

save as myscript.vbs and

C:\test> cscript //nologo myscript.vbs