Search code examples
batch-filefile-renamebatch-rename

Batch file rename reading old file name and new file name from a txt file


I have done some searching and reading but I have not been able to find a solution. I apologize if this issue is already answered on the forum. Here is what I want to do:

I have many 1000's of files that I need to rename. I can easily create a .txt file that lists the old file name and the new file name as follows:

oldfilename1.ext newfilename1.ext
oldfilename2.ext newfilename2.ext
oldfilename3.ext newfilename3.ext

etc......

The actual file names will be as follows:

TKP-RL-MB-00205-001.pdf SDY-352-20009SA10BA-RXW03-001.pdf
TKP-RL-MB-00060-004.pdf SDY-352-20010SA10BA-RXW03-004.pdf

etc....

I would like a batch file that reads the two names from the txt file and renames the file from the old name to the new name. The batch file will be located in the same directory as all the files with the old file name. I don't need functionality to check if the old file exists unless that functionality is essential to the batch file working.

I am working in Microsoft Windows 7 so I have cmd and Powershell at my disposal.

Your help is greatly appreciated.

Thank you


Solution

  • This should do what you have described. Remove the echo after the do when you have tested it - atm it will merely print the commands to the screen.

    @echo off
    for /f "usebackq tokens=1,*" %%a in ("file.txt") do echo ren "%%a" "%%b"
    pause