I'm looking for way to rename multiple files with a batch job. Yes, I know there are many Applications around which can achieve this. But I need an automated way, something like a Batch File which I can call from a Scheduler (SOS Job Scheduler). We need to rename hundreds of files daily!
我正在寻找用批处理作业重命名多个文件的方法。是的,我知道有很多应用程序可以实现这一点。但是我需要一种自动化的方式,比如可以从调度程序(SOS作业调度程序)调用的批处理文件。我们需要每天重命名数百个文件!
The Goal is to set the 17-25 charcaters at the beginning of the file.
目标是在文件的开头设置17-25个charcaters。
- 00010028229720270014468393_TB-E.pdf -> 00144683930001002822972027_TB-E.pdf
- 00010028229720270014468393 _tb-e。pdf - > 00144683930001002822972027 _tb-e.pdf
- 000100282297202700144683931ESR-AF.pdf -> 001446839300010028229720271ESR-AF.pdf
- 000100282297202700144683931 esr-af。pdf - > 001446839300010028229720271 esr-af.pdf
- 00010031141040250016353371ESR-AF.pdf -> 00163533700010031141040251ESR-AF.pdf
- 00010031141040250016353371 esr-af。pdf - > 00163533700010031141040251 esr-af.pdf
- 0001003167580004001667217KTO.pdf -> 0016672170001003167580004KTO.pdf
- 0001003167580004001667217遗传。pdf - > 0016672170001003167580004 kto.pdf
Here an example which is more clearer:
这里有一个更清楚的例子:
0001 002822972 027 001446839 _TB-E .pdf -> 001446839 0001 002822972 027 _TB-E .pdf
0001 002822972 027 001446839 _tb .pdf -> 001446839 0001 002822972 027 _tb .pdf
3 个解决方案
#1
4
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET "name=%%a"
CALL :transform
)
GOTO :EOF
:transform
ECHO REN "%sourcedir%\%name%" "%name:~16,9%%name:~0,16%%name:~25%"
GOTO :eof
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO REN
to REN
to actually rename the files.
所需要的REN命令只是为了测试目的而回显。在验证命令是否正确之后,将ECHO REN更改为REN以实际重命名文件。
Note that the very first example you've presented has ...393_TB-E... in the transformed version, that 3
is missing.
注意,您给出的第一个示例是…393_TB-E…在转换后的版本中,缺少3。
#2
1
This can be accomplished very simply with the help of REPL.BAT - a hybrid JScript/batch utility that performs a regex search and replace on each line from stdin and writes the result to stdout. The utility uses only native scripting capabilities that are available to any modern Windows machine from XP onward; no 3rd party executable required. Complete documentation is embedded within the script.
这可以在REPL的帮助下非常简单地完成。BAT——一个混合的JScript/batch实用程序,在stdin的每一行上执行regex搜索和替换,并将结果写入stdout。该实用程序只使用本地脚本功能,可以从XP开始使用任何现代Windows机器;不需要第三方可执行文件。完整的文档嵌入在脚本中。
Assuming REPL.BAT is somewhere within your PATH:
假设REPL。蝙蝠在你的路径中:
@echo off
pushd "c:\sourcePath"
for /f "delims=" %%A in (
'dir /b /a-d *.pdf ^| repl "(.{16})(.{9}).*" "ren \q$&\q \q$2$1*\q" x'
) do %%A
popd
Using only native batch commands, without any CALL:
只使用本地批处理命令,不调用:
@echo off
setlocal disableDelayedExpansion
pushd "c:\sourcePath"
for /f "delims=" %%F in ('dir /b /a-d *.pdf') do (
set "file=%%F"
setlocal enableDelayedExpansion
ren "!file!" "!file:~16,9!!file:~0,17!*"
endlocal
)
popd
If you know that none of your file names contain the !
character, then you can simply enable delayed expansion at the top, and remove both SETLOCAL and ENDLOCAL from within the loop.
如果您知道您的文件名中没有包含!然后,您可以在顶部启用延迟扩展,并从循环中删除SETLOCAL和ENDLOCAL。
Both solutions above rely on the fact that *
at the end of the target name will preserve the remainder of the original file name. See How does the Windows RENAME command interpret wildcards? for more info.
上述两种解决方案都依赖于以下事实:目标名称末尾的*将保留原始文件名的其余部分。查看Windows重命名命令如何解释通配符?更多信息。
#3
-2
(\d{16})(\d+)(.*?\.pdf) -> \2\1\3
{16} means you take 16 repetitions (of a digit)
{16}表示你重复16次(一个数字)
#1
4
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET "name=%%a"
CALL :transform
)
GOTO :EOF
:transform
ECHO REN "%sourcedir%\%name%" "%name:~16,9%%name:~0,16%%name:~25%"
GOTO :eof
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO REN
to REN
to actually rename the files.
所需要的REN命令只是为了测试目的而回显。在验证命令是否正确之后,将ECHO REN更改为REN以实际重命名文件。
Note that the very first example you've presented has ...393_TB-E... in the transformed version, that 3
is missing.
注意,您给出的第一个示例是…393_TB-E…在转换后的版本中,缺少3。
#2
1
This can be accomplished very simply with the help of REPL.BAT - a hybrid JScript/batch utility that performs a regex search and replace on each line from stdin and writes the result to stdout. The utility uses only native scripting capabilities that are available to any modern Windows machine from XP onward; no 3rd party executable required. Complete documentation is embedded within the script.
这可以在REPL的帮助下非常简单地完成。BAT——一个混合的JScript/batch实用程序,在stdin的每一行上执行regex搜索和替换,并将结果写入stdout。该实用程序只使用本地脚本功能,可以从XP开始使用任何现代Windows机器;不需要第三方可执行文件。完整的文档嵌入在脚本中。
Assuming REPL.BAT is somewhere within your PATH:
假设REPL。蝙蝠在你的路径中:
@echo off
pushd "c:\sourcePath"
for /f "delims=" %%A in (
'dir /b /a-d *.pdf ^| repl "(.{16})(.{9}).*" "ren \q$&\q \q$2$1*\q" x'
) do %%A
popd
Using only native batch commands, without any CALL:
只使用本地批处理命令,不调用:
@echo off
setlocal disableDelayedExpansion
pushd "c:\sourcePath"
for /f "delims=" %%F in ('dir /b /a-d *.pdf') do (
set "file=%%F"
setlocal enableDelayedExpansion
ren "!file!" "!file:~16,9!!file:~0,17!*"
endlocal
)
popd
If you know that none of your file names contain the !
character, then you can simply enable delayed expansion at the top, and remove both SETLOCAL and ENDLOCAL from within the loop.
如果您知道您的文件名中没有包含!然后,您可以在顶部启用延迟扩展,并从循环中删除SETLOCAL和ENDLOCAL。
Both solutions above rely on the fact that *
at the end of the target name will preserve the remainder of the original file name. See How does the Windows RENAME command interpret wildcards? for more info.
上述两种解决方案都依赖于以下事实:目标名称末尾的*将保留原始文件名的其余部分。查看Windows重命名命令如何解释通配符?更多信息。
#3
-2
(\d{16})(\d+)(.*?\.pdf) -> \2\1\3
{16} means you take 16 repetitions (of a digit)
{16}表示你重复16次(一个数字)