开发者

Rename Multiple files with in Dos batch file

I wish to rena开发者_StackOverflowme all files inside the folder *.txt, so the result will be "1.txt", "2.txt" and "3.txt", ....

How can I do so?


The following may accomplish what you are looking for. It uses a for loop to iterate through the text files and makes a "call" to another bit of the batch file to do the rename and increment of a variable.

Edit Change math operation to cleaner solution suggested by Andriy.

@echo off
set i=1
for %%f in (*.txt) do call :renameit "%%f"
goto done

:renameit
ren %1 %i%.txt
set /A i+=1

:done


First make a directory listing:

dir /b *.txt > myfile.cmd

Then start up UltraEdit (http://www.ultraedit.com/) and open the file.

Then go into column mode, select all lines, and:

  • insert "RENAME " in the beginning of every line
  • insert ".TXT" at the end of every line (be sure to put it far enough right in case you have very long lines)
  • insert a number (see Column / Insert Number in the menu) right before .TXT


I wish to rename all files inside the folder *.txt, so the result will be "1.txt", "2.txt" and "3.txt", ....

How can I do so?

::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\This\Is\The\Folder
SET count=1

::Action
CD "%folder%"
FOR %%F IN ("*.txt") DO (
 MOVE "%%F" "!count!.txt"
 SET /a count=!count!+1
)
ENDLOCAL

Shorthand

SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR %%F IN (C:\Path\To\File\*.txt) DO MOVE "%%~fF" "%%~dpF!count!.txt" & SET /a count=!count!+1
ENDLOCAL

So if your folder contained cat.txt, dog.txt, bird.txt, ninjaturtle.txt, it will output 1.txt, 2.txt, 3.txt, 4.txt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜