windows 在 Dos 批处理文件中重命名多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6359587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:28:27  来源:igfitidea点击:

Rename Multiple files with in Dos batch file

windowsbatch-filedos

提问by JustMe

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

我想重命名文件夹 *.txt 中的所有文件,所以结果将是“1.txt”、“2.txt”和“3.txt”,....

How can I do so?

我怎么能这样做?

回答by Mark Wilkins

The following may accomplish what you are looking for. It uses a forloop 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.

以下内容可能会完成您正在寻找的内容。它使用for循环来遍历文本文件,并对批处理文件的另一位进行“调用”以进行变量的重命名和增量。

EditChange math operation to cleaner solution suggested by Andriy.

编辑将数学运算更改为 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

回答by Patrick

First make a directory listing:

首先列出目录:

dir /b *.txt > myfile.cmd

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

然后启动 UltraEdit ( http://www.ultraedit.com/) 并打开文件。

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
  • 在每一行的开头插入“RENAME”
  • 在每一行的末尾插入“.TXT”(确保把它放得足够远,以防你有很长的行)
  • 在 .TXT 之前插入一个数字(请参阅菜单中的列/插入数字)

回答by Mechaflash

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

我想重命名文件夹 *.txt 中的所有文件,所以结果将是“1.txt”、“2.txt”和“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.

所以如果你的文件夹包含cat.txt、dog.txt、bird.txt、ninjaturtle.txt,它会输出1.txt、2.txt、3.txt、4.txt。