3

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

AlexB
  • 7,302
  • 12
  • 56
  • 74
Adrian
  • 33
  • 1
  • 4

3 Answers3

4

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thank you so much. This has worked perfectly. Just for clarity for future readers I removed the @echo off statement and the echo after do so that my file read as follows: for /f "usebackq tokens=1,*" %%a in ("file.txt") do ren "%%a" "%%b – Adrian Apr 25 '14 at 08:19
  • @foxidrive, could you, please, clarify of what encoding should be that file (or how to change encoding for script)? I have file names of non-Latin characters, and they became unreadable after your script. – Suncatcher Mar 21 '16 at 20:43
  • @Suncatcher The codepage used by a batch file may help, with the `cp [number]` command, but non-Latin characters can be difficult to handle. I don't have much experience with them myself. – foxidrive Mar 23 '16 at 19:21
  • I resolved the issue by putting `chcp 65001` command before your script. Encoding of txt with filenames also matters, it should be UTF-8 in that case. Thanks for assistance. – Suncatcher Mar 24 '16 at 11:48
  • @Suncatcher Thanks for your feedback, and I see you got the command name right. :) Just adding here that UTF-8 format files aren't always processed correctly by batch code. – foxidrive Mar 24 '16 at 23:16
1
for /f "tokens=1,2" %%a in (yourtextfile.txt) do ECHO ren "%%a" "%%b"

-assuming that the only spaces in the file are those separating the names.

Reduce %% to % to execute directly from the prompt.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Yes, thank you. I realise that now and I have put the @echo off back at the start of the file. I had a small issue with UNC paths on a network drive and after reading another post I included a line pushd \\UNC path ahead of the for statement and a popd after the for statement. This solved the UNC problem. The other post is here: [link](http://stackoverflow.com/questions/9013941/how-to-run-batch-file-from-network-share-without-unc-path-are-not-supported-me) The post by dbenham helped me. – Adrian Apr 25 '14 at 11:59
1

If you have that textfile, you might just put a rename at the beginning of each line and save it as .bat.

(E. g. with Notepad++, Search Mode = Extended, replace \n with \nrename, maybe check first and last lines)

KekuSemau
  • 6,830
  • 4
  • 24
  • 34
  • Thank you. I prefer foxidrive's approach because I don't always generate the txt file with the file names myself. It is often done by others and foxidrive's approach gives me a batch file that I can share with others and they can simply use on their own lists of files. Nevertheless, thank you. – Adrian Apr 25 '14 at 11:55