以递归方式批量重命名文件扩展名(Windows 批处理)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17658354/
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
Mass rename of file extensions recursively (windows batch)
提问by Raiden616
I have numerous files in a very complex directory structure, and for reasons not worth discussing I need to rename all files with the extension of ".inp" to have ".TXT" extensions. There are numerous other files with other extensions that I do not want to be touched, and I want to do it recursively down at least 5 levels.
我在一个非常复杂的目录结构中有很多文件,出于不值得讨论的原因,我需要将所有扩展名为“.inp”的文件重命名为“.TXT”扩展名。还有许多其他扩展名的文件,我不想被触及,我想递归地执行至少 5 个级别。
So far I have:
到目前为止,我有:
for /d %%x in (*) do pushd %%x & Ren *.inp *.TXT & popd
...but this only goes down one level of directories.
...但这只会下降一级目录。
Can anyone help? Thanks in advance!
任何人都可以帮忙吗?提前致谢!
回答by john smith
On Windows 7, the following one-line command works for me, to rename all files, recursively, in *.js to *.txt:
在 Windows 7 上,以下单行命令适用于我,以递归方式将 *.js 中的所有文件重命名为 *.txt:
FOR /R %x IN (*.js) DO ren "%x" *.txt
回答by Magoo
for /r startdir %%i in (*.inp) do ECHO ren "%%i" "%%~ni.txt"
should work for you. Replace startdir
with your starting directoryname and when you've checked this works to your satisfaction, remove the echo
before the ren
to actually do the rename.
应该为你工作。替换startdir
为您的起始目录名,当您检查这是否符合您的要求时,删除echo
之前的ren
以实际进行重命名。
For the downvoters: executing a batch file differs from excuting from the command prompt in that each %%x
where x
is the metavariable (loop-control variable) needs to be reduced to %
, so
对于downvoters:执行批处理文件与从命令提示符执行不同之处在于,每个%%x
wherex
是元变量(循环控制变量)都需要减少到%
,所以
for /r startdir %i in (*.inp) do ECHO ren "%i" "%~ni.txt"
should work if you execute this from the prompt. Please read the note about echo
.
如果您从提示中执行此操作,则应该可以工作。请阅读关于 的说明echo
。
回答by Roger Hill
John Smith's answer is excellent, and it works. But to be completely clear (I had to re-read magoo's notes to figure out the correct syntax), here is exactly what you need to do...
约翰史密斯的回答非常好,而且很有效。但是要完全清楚(我不得不重新阅读 magoo 的笔记以找出正确的语法),这正是您需要做的......
BATCH FILE:
FOR /R %%x IN (*.js) DO ren "%%x" *.txt
COMMAND LINE:
FOR /R %x IN (*.js) DO ren "%x" *.txt
Up vote their responses, I am but a lowly formater...
对他们的回答投票,我只是一个卑微的格式化者......