Вопрос

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

Это было полезно?

Решение

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

Другие советы

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.

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)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top