批处理脚本可反转文件名中日期的顺序 [英] Batch script to invert the order of a date in file names
问题描述
我想使用批处理脚本来重命名使用以下命名方案的一堆文件:
I wanted to use a batch script to rename a bunch of files which are using the following name scheme:
File 2-9.pdf
File 3-9.pdf
File 4-9.pdf
[...]
我想反转数字,以便它们变为...
I want to invert the numbers so that they will become...
File 9-2.pdf
File 9-3.pdf
File 9-4.pdf
[...]
通常,我会搜索重复的字符串并将其替换,但是在这种情况下,字符串随每个文件而变化,因此我不知道该如何处理.有什么想法吗?
Normally I would search for the repeating string and replace it, but in this case the string changes with each file, so I don't know how to go about it. Any ideas?
感谢您的阅读.
我在Windows上,正在寻求创建.bat文件.像
I am on Windows, I am seeking to create a .bat file. Something like
For %%# in ("file-path") Do (
Set "File=%%~nx#"
Ren "%%#" "!File:%Pattern%=%Replace%!"
)
推荐答案
相对容易.不要使用搜索并替换",而是使用适当的标记和定界符来分割文件名:
relatively easy. Don't use "search and replace", but use proper tokens and delimiters to split your filenames:
@echo off
for %%f in (*.pdf) do (
for /f "tokens=1,2,3 delims=- " %%a in ("%%~nf") do (
ECHO ren "%%~f" "%%a %%c-%%b%%~xf"
)
)
(注意:故障排除后请删除 ECHO
以启用重命名命令)
(Note: remove the ECHO
after troubleshooting to enable the rename command)
这篇关于批处理脚本可反转文件名中日期的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!