用于重命名多个文件夹的 .bat 文件 [英] .bat file for renaming multiple folders
问题描述
我正在尝试编写一个批处理脚本来重命名多个文件夹.我想做如下事情:通过在文件夹名称的末尾附加我的名字来重命名Workspace"文件夹下的所有文件夹
I am trying to write a batch script to rename multiple folders. I would like to do something like below: Rename all folders under the "Workspace" folder by appending my name in the end of the folder names
例如重命名:
Workspace/RiskFolder
Workspace/PNLFolder
到:
Workspace/RiskFolder_myname
Workspace/PNLFolder_myname
这可能吗?
推荐答案
您可以使用 for 循环遍历每个目录并像这样重命名:
You could use for to loop through each directory and rename it like so:
for /D %%f in (C:path oWorkspace*) do rename "%%f" "%%~nxf_myname"
我在 Windows 7 上测试过这个,但它应该至少可以在 Windows XP 上工作.
I tested this on Windows 7, but it should work at least as far back as with Windows XP.
这样做的是:对于路径中的每个目录(在括号内),将目录名称分配给变量%%f
,然后重命名目录%%f
到您想要的格式的名称(附上您的姓名).%%f
保存完整的路径名,这对于 rename
命令的第一个参数来说很好,但是对于第二个参数,我们只需要文件名+扩展名,因此~nx 修饰符加在我们的变量名前面.
What that does is this: for each directory in the path (within parenthesis), assign the directory name to the variable %%f
, then rename the directory %%f
to the name in the format you want (with your name attached). %%f
holds the full pathname, which is fine for the first argument to the rename
command, but for the second argument, we only want the filename+extension, thus the ~nx modifier prepended to our variable name.
顺便说一下,当在命令行(而不是批处理文件的一部分)上使用这个 for
循环时,您只想使用 % 而不是 %% 作为变量名.例如.for %f in...
而不是上面的.
By the way, when using this for
loop on the command line (rather than part of a batch file) you only want to use one % instead of %% for your variable name. E.g. for %f in...
instead of above.
有关详细信息,请参阅以下来自 Microsoft 的参考资料:
See the following references from Microsoft for more details:
- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
- http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true
这篇关于用于重命名多个文件夹的 .bat 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!