Rename multiple files in a directory using batch script

家住魔仙堡 提交于 2019-12-09 11:47:54

问题


I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...

for /r %%i in (*) do (
    echo %c% 
)

回答1:


Here's the script. Just put the script in your folder and run it.

@echo off & setlocal EnableDelayedExpansion 

set a=1
for /f "delims=" %%i in ('dir /b *') do (
  if not "%%~nxi"=="%~nx0" (
    ren "%%i" "!a!" 
    set /a a+=1
 ) 
) 

If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.

@echo off & setlocal EnableDelayedExpansion 

set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
  ren "%%i" "!a!.jpg" 
  set /a a+=1
) 

[Update] Here're explanations for the lines mentioned in Jack's comment.

  • setlocal EnableDelayedExpansion

In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.

For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.

  • for /f "delims=" %%i in ('dir /b *.jpg')

Here dir with /b option, lists only file names of all jpg files.

The for loop traverses and renames all jpg files.

For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.

For for loop, please refer to the page http://ss64.com/nt/for_f.html.

  • if not "%%~ni"=="%~n0"

I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.

It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.

%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.




回答2:


There is no need for a batch script. A simple one liner from the command line can do the job :-)

I use DIR /B to generate the list of files, piped to FINDSTR to number the files, all enclosed withn FOR /F to parse the result and perform the rename.

for /f "delims=: tokens=1*" %A in ('dir /b *.jpg^|findstr /n "^"') do @ren "%B" "%A%~xB"

Double the percents if you want to put the command in a batch script.




回答3:


Try this, you have pair of namevalues in a text file then loop values and do the magic. Namevalues are separated by empty spaces. This allows you to map old->new filenames accordingly. Or you keep idx+1 counter and use it for new filenames.

keyvalue.bat

@echo off
set idx=0
for /F "tokens=1,2" %%A in (keyvalue.txt) do call :PROCESS "%%A"  "%%B"
GOTO :END

:PROCESS
set var1=%~1
set var2=%~2
set /A idx=%idx%+1
echo %var1% goes to %var2% (%idx%)
GOTO :EOF

:END
pause

keyvalue.txt

file888.dat  newfile1.dat
file333.dat  newfile2.dat
file9.dat    newfile3.dat
file01.dat   newfile4.dat


来源:https://stackoverflow.com/questions/25583161/rename-multiple-files-in-a-directory-using-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!