英文:
how to rename multiple sub directories of a parent folder with a script?
问题
以下是您要翻译的部分:
我有一个包含多个子目录的父目录,这些子目录包含与它们相同的文件夹名称,需要将它们重命名为相同的名称。我想在父目录中放置一个脚本,该脚本将遍历子文件夹并根据匹配项重命名目录。例如,在每个名为Foo的文件夹中将其重命名为Apple。
我有一个不起作用的脚本如下:
FOR /D %%D IN (*.*) DO (
REN "Foo" "Apple"
REN "Bar" "Banana"
REN "Baz" "Mango"
)
在for循环内,该脚本试图重命名文件。如果我将下面的行放在一个.cmd文件中,它将重命名文件夹:
REN "Foo" "Apple"
我想将脚本放在父目录中,并让它搜索子文件夹,并将所有Foo文件夹重命名为Apple,所有Bar文件夹重命名为Banana,所有Baz文件夹重命名为Mango。
当前文件夹结构:
| 父目录
| |--子目录1
| | |--Foo
| | |--Bar
| | |--Baz
| |--子目录2
| | |--Foo
| | |--Bar
| | |--Baz
| |--子目录3
| | |--Foo
| | |--Bar
| | |--Baz
重命名后的文件夹结构:
| 父目录
| |--子目录1
| | |--Apple
| | |--Banana
| | |--Mango
| |--子目录2
| | |--Apple
| | |--Banana
| | |--Mango
| |--子目录3
| | |--Apple
| | |--Banana
| | |--Mango
感谢任何帮助!
英文:
I have parent folder with several sub directories which contain folders named the same to be renamed to the same name. I would like place a script at the parent folder that will traverse through the subfolders and rename directories according to a match. For instance in each folder with a folder name Foo rename it to Apple.
the script I have not working is
FOR /D %%D IN (*.*) DO (
REN "Foo" "Apple"
REN "Bar" "Banana"
REN "Baz" "Mango"
)
inside the for loop that script is trying to rename files. If I place the lines below in a .cmd file it will rename folders
REN "Foo" "Apple"
I would like to place the script in the parent folder and have it search through the sub folders and rename all the Foo folders to Apple, all the Bar folders to Banana, and all the Baz folders to Mango
Current Folder Tree:
| Parent Folder
| |--Sub Folder 1
| | |--Foo
| | |--Bar
| | |--Baz
| |--Sub Folder 2
| | |--Foo
| | |--Bar
| | |--Baz
| |--Sub Folder 3
| | |--Foo
| | |--Bar
| | |--Baz
Renamed Folder Tree would be:
| Parent Folder
| |--Sub Folder 1
| | |--Apple
| | |--Banana
| | |--Mango
| |--Sub Folder 2
| | |--Apple
| | |--Banana
| | |--Mango
| |--Sub Folder 3
| | |--Apple
| | |--Banana
| | |--Mango
Thanks for any help!
答案1
得分: 1
Here's the translated code portion:
[未经测试]
for /d /r %%e in (*.*) do (
echo ren "%%e\Foo" "Apple" 2>nul
echo ren "%%e\Bar" "Banana" 2>nul
...
)
`ren` 命令被 `echo` 到控制台以进行验证。
如果结果列表是您想要执行的操作,请删除 `echo` 关键字以激活重命名。
`2>nul` 应该抑制错误消息(比如 `Foo` 不存在)。
英文:
[untested]
for /d /r %%e in (*.*) do (
echo ren "%%e\Foo" "Apple" 2>nul
echo ren "%%e\Bar" "Banana" 2>nul
...
)
The ren
commands are echo
ed to the console for verification purposes.
If the resultant list is what you want to do, remove the echo
keywords to activate the rename.
The 2>nul
should suppress error messages (like if Foo
does not exist)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论