![在Windows cmd下重命名日期](https://linux22.com/image/1338686/%E5%9C%A8Windows%20cmd%E4%B8%8B%E9%87%8D%E5%91%BD%E5%90%8D%E6%97%A5%E6%9C%9F.png)
我有一堆目录(如果你愿意的话可以叫它文件夹)遵循这种模式
\20121022 Description of the directory's contents goes here\
(有些没有,只有日期)
我需要重命名它们以遵循以下模式
\2012-10-22 Description of the directory's contents ...\
有没有办法使用 Windows cmd 及其附带的工具(即 ren)来做到这一点?如果没有,最便携的方法是什么?我在执行此操作的机器上拥有一组非常受限制的权限。
答案1
如果以变量中的目录名称开始,例如 fdir:
set "fdir=20121022 Description of this directory"
然后您的批处理文件可以拆分目录名称:
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
此时,变量如下所示:
fdir=20121022 Description of this directory
fdiryear=2012
fdirmonth=10
fdirday=22
fdirdesc= Description of this directory
然后定义新的目录名称,并重命名该目录:
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
ren "%fdir%" "%fnew%"
新的目录名称将是:
fnew=2012-10-22 Description of this directory
如果您能给我提供更多有关如何收集目录名称列表的信息,我可以提供更完整的脚本。
例如,如果所有目录都在当前目录中,则批处理文件将如下所示:
@echo off
echo.
for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"
echo.
set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="
goto :EOF
:work
set fdir=%~1
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
echo Renaming folder "%fdir%" to "%fnew%"
rem ren "%fdir%" "%fnew%"
goto :EOF
解释:
这将关闭执行命令时的回显,以避免屏幕上出现大量混乱:
@echo off
这将显示一个空白行:
echo.
for 命令:
for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"
/D means to search for matches of Directory names only.
If you omit the /D, it will search for matches of Filenames only.
%%f (%%~f) is a variable name that gets set to the names of the matched directories.
[in ("2010*", "2011*", "2012*")] is a list of patterns to search for.
In this case, it will only process directories that begin with a year:
2010, or 2011, or 2012. You can edit this list for your needs. If you
know that all directories in the current folder are in the same format
and are candidates to be renamed, the list can be simply: ("*") like:
for /D %%f in ("*") do call :work "%%~f"
the word "do" simply precedes the command that you want to "do" for each
match found.
call :work says to execute the commands at the label :work for each match.
"%%~f" says to pass the matched directory name as the first argument to the
commands at the label you specified (:work)
The "~" in "%%~f" says to remove "quote" marks from %%f to avoid passing the
directory name inside of 2 sets of "quote" marks. For example, if %%f contained
"20121022 Description of this directory" then using "%%f" would pass
""20121022 Description of this directory"", which would fail. "%%~f" will pass
exactly 1 set of "quotes".
处理完所有匹配的目录名后,执行返回到“for ...”命令后面的“echo.”命令。
接下来只是一些“清理”来清除脚本中使用的变量,这样它们就不会积累并扰乱变量空间(环境)。
set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="
下一个语句退出批处理脚本并结束执行。
goto :EOF
接下来是标签
:work
Labels begin with a colon at the first column followed by a
label name made up of letters and numbers (no spaces). Labels are
the targets of call and goto commands.
然后命令:
set fdir=%~1
Sets the variable named "fdir" to be the directory name that was
passed from the "for" command.
The "~" in %~1 means to use the name only without any surrounding "quote" marks.
下一个命令:
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
Split up the directory name into the desired pieces, and
define the "new" directory name.
下一个命令:
echo Renaming folder "%fdir%" to "%fnew%"
Just displays the old and new directory names so you can see the
progress on the screen.
最后的命令:
rem ren "%fdir%" "%fnew%"
goto :EOF
Renames the directory and then "exits" to return from the call.
笔记:我将实际的重命名目录命令注释掉,这样您就可以运行批处理文件,看看会发生什么,而无需实际重命名任何内容。一旦您运行了它并确信正确的目录将被正确重命名,您就可以编辑该行以重命名目录并再次运行批处理脚本。
为此,请从该行中删除“rem”,如下所示:
rem ren "%fdir%" "%fnew%"
Becomes:
ren "%fdir%" "%fnew%"