简体   繁体   English

递归批量重命名文件扩展名(Windows批处理)

[英]Mass rename of file extensions recursively (windows batch)

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.我在一个非常复杂的目录结构中有许多文件,出于不值得讨论的原因,我需要将所有带有“.inp”扩展名的文件重命名为“.TXT”扩展名。 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.还有许多其他文件具有其他扩展名,我不想被触及,我想递归地执行至少 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!提前致谢!

在 Windows 7 上,以下单行命令适用于我,以递归方式将 *.js 中的所有文件重命名为 *.txt:

FOR /R %x IN (*.js) DO ren "%x" *.txt
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替换为您的起始目录名,当您检查它是否满意时,删除ren之前的echo以实际进行重命名。


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其中x是元变量(循环控制变量)需要减少到% ,所以

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的说明。

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...投票赞成他们的回答,我只是一个卑微的形成者......

Sometimes the renaming is necessary to change invalid characters inside the files and names.有时需要重命名以更改文件和名称中的无效字符。 If you need to change a single character recursively:如果您需要递归更改单个字符:

Change character "╞" for "ã" in folder names recursively:递归地更改文件夹名称中“ã”的字符“╞”:

FOR /R /D %x in (*╞*) do @for /f "tokens=1-3* delims=╞" %B in ("%x") DO ren "%x" "%~nBã%C"

Change character "╞" for "ã" in file names recursively:递归地更改文件名中“ã”的字符“╞”:

FOR /R %x in (*╞*.*) do @for /f "tokens=1-3* delims=╞" %B in ("%x") DO ren "%x" "%~nBã%C"

Change the chars for whatever you need.根据需要更改字符。 If you want to test your renaming commands before executing, just put an @echo between DO and ren:如果你想在执行之前测试你的重命名命令,只需在 DO 和 ren 之间添加一个@echo:

DO @echo ren "%x" "%~nBã%C"

This will output the commands instead of executing them这将输出命令而不是执行它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM