Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

windows - Rename Multiple files with in Dos batch file

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...