【发布时间】:2018-11-14 17:10:00
【问题描述】:
我正在尝试重命名目录中的一组文件,这些文件都包含我稍后要删除的文件名的一部分。 名字的例子是:
- 123876.111thepartIwanttoremove.exe
- thisisatestfile.111thepartIwanttoremove.exe
this?392!.111thepartIwanttoremove.exe
thankyouall.222thepartIwanttoremove.exe
- test.222thepartIwanttoremove.exe
- whatis@d354.222thepartIwanttoremove.exe
我的代码是:
forfiles /S /M *.111thepartIwanttoremove.exe /C "cmd /c rename @file @fname.doc"
ren ???.111thepartIwanttoremove.* ???.doc
ren ????.111thepartIwanttoremove.* ????.doc
ren ?????.111thepartIwanttoremove.* ?????.doc (and so on)
forfiles /S /M *.222thepartIwanttoremove.exe /C "cmd /c rename @file @fname.jpg"
ren ???.222thepartIwanttoremove.* ???.jpg
ren ????.222thepartIwanttoremove.* ????.jpg
ren ?????.222thepartIwanttoremove.* ?????.jpg (and so on)
举个例子,我想要文件:
123876.111thepartIwanttoremove.exe
看起来像这样:
123876.doc
之后我可以使用什么函数来删除名称的 .*thepartIwanttoremove.exe 部分而不用“?”写这么多行?
非常感谢您的帮助。
【问题讨论】:
-
变量扩展和替换是您可以使用的功能。您可能希望使用循环来执行此操作,并且根据循环类型可能会使用延迟扩展。
-
请发布真实数据。目前尚不清楚是否需要从名称中删除某些特定字符串,或者名称在扩展名的前 3 个字符之后被截断,或者在扩展名中的第一个数字字符串之后被截断。实际上,您的示例是
.exe文件,但您的代码面向.jpg文件。 -
这是真实数据。扩展名为三个数字的文件 (123876.111thepartIwanttoremove.exe) 必须重命名为
.doc文件,而具有三个数字的文件 (thankyouall.222thepartIwanttoremove.exe) 必须重命名为.jpg文件但没有中间部分。 123876.111thepartIwanttoremove.exe ---> 123876.docthankyouall.222thepartIwanttoremove.exe --->thankyouall.jpg -
绝对不需要 FORFILES。您最初问题中的示例使您看起来需要在第一个句点之后保留前三个数字。但是现在您在评论中的示例是在第一期之后放弃所有内容。如果是这种情况,您可以利用 FOR 命令中命令修饰符的强大功能。
标签: windows file batch-file rename filenames