我有以下脚本:
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
if %ThisService_filerestoreoption% == 1 (
if exist "%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%" rmdir /s /q "%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%"
ROBOCOPY D:\file-backups\%ThisUser_Username%\Mission\%ThisService_mapname% %ThisService_WorkingDirectory%MPMissions\%ThisService_mapname% /minage:%ThisService_filerestoreage% /maxage:%ThisService_filerestoreage% /e
ren "%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%_*" "%ThisService_mapname%"
ROBOCOPY D:\file-backups\%ThisUser_Username%\Server %ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons dayz_server_*.pbo /minage:%ThisService_filerestoreage% /maxage:%ThisService_filerestoreage%
if exist "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server.pbo" DEL "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server.pbo"
ren "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server_*.pbo" "dayz_server.pbo"
)
if %ThisService_filerestoreoption% == 2 (
ROBOCOPY D:\file-backups\%ThisUser_Username%\Server %ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons dayz_server_*.pbo /minage:%ThisService_filerestoreage% /maxage:%ThisService_filerestoreage%
if exist "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server.pbo" DEL "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server.pbo"
ren "%ThisService_WorkingDirectory%@DayZ_Epoch_Server\Addons\dayz_server_*.pbo" "dayz_server.pbo"
)
我需要能够在第一个if语句中重命名该文件夹:
ren "%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%_*" "%ThisService_mapname%"
问题是它不会找到与此模式匹配的文件夹,如果它是一个文件它没有问题,但这甚至可以用文件夹吗?
由于
答案 0 :(得分:0)
REN命令可怕地记录在案,特别是关于它如何处理通配符。我已经进行了广泛的实验并提出了规则:在SuperUser上查看How does the Windows RENAME command interpret wildcards?。
不幸的是,REN命令不会使用源名称或目标名称中的任何通配符重命名文件夹。
在查看代码时,您似乎确信源掩码永远不会匹配多个文件夹。我假设你是对的。
您可以执行以下操作来解决问题:
for /d %%F in ("%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%_*") do ren "%%F" "%ThisService_mapname%"
另一个选项是我的JREN.BAT utility - 一个混合JScript /批处理脚本,它通过正则表达式搜索重命名文件或文件夹并替换名称。
call jren "^.*" "%ThisService_mapname%" /d /p "%ThisService_WorkingDirectory%MPMissions" /fm "%ThisService_mapname%_*"
答案 1 :(得分:0)
使用move
代替ren
move "%ThisService_WorkingDirectory%MPMissions\%ThisService_mapname%_*" "%ThisService_mapname%"
请注意,如果有任何文件与该通配符匹配,move
和ren
将重命名文件并保留目录。在这种情况下,@ dbenham的FOR /D
解决方案仍然适用。
答案 2 :(得分:0)
一个可能更容易使用的解决方案是我之前写过的命令行JScript脚本:
Windows IT Pro: Rename Files by Using Regular Expressions
用法是:
[cscript] Renamer.js <directory> [...] [/f:<str>] [/r:<str>] [/o:<d|f|df>] [/c]
[/p] [/s] [/t]
<directory> - Specify one or more directory names on the command line
/f:<str> - Regular expression to find in file/directory names (default=space)
/r:<str> - Replacement string (default=_)
/o:<d|f|df> - What to rename? d=directories, f=files, df=both (default=f)
/c - Case-sensitive pattern matching (don't ignore case)
/p - Prompt to rename each file/directory
/s - Recurse through subdirectories
/t - Test mode: Only display what will be renamed
USE THIS SCRIPT WITH EXTREME CAUTION. It can potentially render a system
unbootable by renaming system files/directories, and it can break applications
by renaming critical files/directories.
The script does not test whether a directory or a file's new name is valid
before attempting to rename it, so test mode (/t) may report inaccurate results
if a new name is not valid.