windows 用于重命名多个文件夹的 .bat 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9271107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.bat file for renaming multiple folders
提问by NewQueries
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
我正在尝试编写一个批处理脚本来重命名多个文件夹。我想做如下操作:通过在文件夹名称的末尾附加我的名字来重命名“工作区”文件夹下的所有文件夹
For example, rename:
例如,重命名:
Workspace/RiskFolder
Workspace/PNLFolder
to:
到:
Workspace/RiskFolder_myname
Workspace/PNLFolder_myname
Is this possible?
这可能吗?
回答by Zeb DeOs
You could use for to loop through each directory and rename it like so:
您可以使用 for 循环遍历每个目录并像这样重命名它:
for /D %%f in (C:\path\to\Workspace\*) do rename "%%f" "%%~nxf_myname"
I tested this on Windows 7, but it should work at least as far back as with Windows XP.
我在 Windows 7 上对此进行了测试,但它应该至少可以在 Windows XP 上运行。
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.
这样做的是:对于路径中的每个目录(在括号内),将目录名称分配给变量%%f
,然后%%f
以您想要的格式将目录重命名为名称(附上您的姓名)。%%f
保存完整路径名,这对于rename
命令的第一个参数很好,但对于第二个参数,我们只需要文件名+扩展名,因此 ~nx 修饰符添加到我们的变量名之前。
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.
顺便说一句,在for
命令行(而不是批处理文件的一部分)上使用此循环时,您只想使用 % 而不是 %% 作为变量名。例如,for %f in...
而不是上面。
See the following references from Microsoft for more details:
有关更多详细信息,请参阅 Microsoft 的以下参考资料:
回答by dbenham
No need for a batch file. This will work from the command line
不需要批处理文件。这将从命令行工作
for /d %D in ("Workspace\*") do ren "%D" "%~nxD_myName"
If you do use a batch file, then %D
must become %%D
如果确实使用批处理文件,则%D
必须成为%%D
回答by vaisakh
You can use the following command within your batch file:-
您可以在批处理文件中使用以下命令:-
for /F "usebackq tokens=*" %%a in (`dir /ad /b %1`) do ren %1\%%a %%a%2
This is the DOS 'for' command, which iterates over given set of items, and for each element in the set, performs the given action. For the given requirement, we need to do the following:-
这是 DOS 的“for”命令,它遍历给定的项目集,并为集合中的每个元素执行给定的操作。对于给定的要求,我们需要执行以下操作:-
1) Accept name of folder which contains sub-folders to be renamed(in your example, it is Workspace).
1)接受包含要重命名的子文件夹的文件夹名称(在您的示例中,它是工作区)。
2) Accept the string to be appended to the end(in your example, it is your name).
2)接受要附加到末尾的字符串(在您的示例中,它是您的名字)。
3) List the names of sub-folders in the folder.
3) 列出文件夹中子文件夹的名称。
4) Rename by appending the string to original name.
4) 通过将字符串附加到原始名称来重命名。
Let's see how this for command accomplishes that. The format of 'for' command used here is:-
让我们看看这个 for 命令是如何实现的。这里使用的“for”命令的格式是:-
for /F ["options"] %variable IN (`command`) do command [command-parameters]
The command here assumes that the required parent directory name and string to be appended are passed on as command line parameters. These are represented by %1 and %2 (first and second parameters).
此处的命令假定所需的父目录名称和要附加的字符串作为命令行参数传递。它们由 %1 和 %2(第一个和第二个参数)表示。
To enable us to issue a dos command to be evaluated, we need to use the /F option. The option string is :-
为了使我们能够发出要评估的 dos 命令,我们需要使用 /F 选项。选项字符串是:-
"usebackq tokens=*"
- usebackq specifies backquouted string is a command to be evaluated.(Note that the dir command is enclosed within backquotes(`) )
- tokens=* means to consider each line as a single token and pass to the command
- usebackq 指定反引号字符串是要评估的命令。(注意 dir 命令包含在反引号(`)中)
- token=* 表示将每一行视为单个标记并传递给命令
To list the sub-directories in parent directory, we use the command:-
要列出父目录中的子目录,我们使用命令:-
dir /ad /b %1
- /ad displays only directories (ignores files)
- /b displays it in bare format, i.e., only names are returned and date, time and other info are not.
- %1 is the command line variable referring to parent directory.
- %%a is the variable which receives the sub-directory name in each iteration. Double percentage symbol is required since we use it in a batch file, otherwise, just one is required (like %a)
- /ad 仅显示目录(忽略文件)
- /b 以裸格式显示,即只返回名称,不返回日期、时间和其他信息。
- %1 是引用父目录的命令行变量。
- %%a 是在每次迭代中接收子目录名称的变量。由于我们在批处理文件中使用它,因此需要双百分比符号,否则只需要一个(如 %a)
Finally, we specify the action to be performed:-
最后,我们指定要执行的操作:-
ren %1\%%a %%a%2
- %1\%%a constructs absolute path to sub-directory
- %%a%2 append second command line parameter to original name
- %1\%%a 构造子目录的绝对路径
- %%a%2 将第二个命令行参数附加到原始名称
For more info on for command, type following in a command prompt:-
有关 for 命令的更多信息,请在命令提示符中键入以下内容:-
for /?
For another usage example, refer Loopy loops: The DOS way
对于另一个使用示例,请参阅Loopy loops: The DOS way
回答by Lampis
If there is no need to perform folder/file renaming with a batch file, you can also use the Bulk Rename Utility for Windows. Check this: http://www.bulkrenameutility.co.uk/Download.php
如果不需要使用批处理文件执行文件夹/文件重命名,您也可以使用适用于 Windows 的批量重命名实用程序。检查这个:http: //www.bulkrenameutility.co.uk/Download.php
回答by NightStorm
For /D %%f in (*) do rename "%%f" "%%fWhatEverNameYouLike"
pause
The pause is to see it! Make a cmd of it and put it in the folder that you want to rename all it's subfolders! They'll rename to each one folders name, plus the WhatEverNameYouLike
暂停是看它!制作一个 cmd 并将其放入您要重命名所有子文件夹的文件夹中!他们将重命名为每个文件夹的名称,以及 WhatEverNameYouLike