Home > OS >  Loop in .bat file
Loop in .bat file

Time:01-31

I must rename more hundred files, they are numbered

on the left, from 01 to 100, on the right side from G3002185 to G3002285

i use this script

rename  01.pdf G3002185.pdf
rename  02.pdf G3002186.pdf
rename  03.pdf G3002187.pdf
rename  04.pdf G3002188.pdf
rename  05.pdf G3002189.pdf
rename  06.pdf G3002130.pdf
(...)

but I would like to shorten it, is there an option to loop it somehow?

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /L %%c IN (1001,1,1100) DO (
 SET "oldname=%%c"
 SET /a newname=3002185 - 1001   %%c
 IF %%c==1100 (SET "oldname=!oldname:~-3!") ELSE (SET "oldname=!oldname:~-2!")
 ECHO REN "!oldname!.pdf" "G!newname!.pdf"
)
GOTO :EOF

Always verify against a test directory before applying to real data.

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.

Your example appears to be faulty. "06" should be renamed to "G3002190".

And your count is incorrect. The last new-filename should be "G3002284.pdf"

CodePudding user response:

this must help

for /L %%x in (1,1,200) do ren "file_%%x.txt" "new_file_%%x.txt"

  • Related