如何使用脚本重命名父文件夹的多个子目录?

huangapple go评论63阅读模式
英文:

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 echoed 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)

huangapple
  • 本文由 发表于 2023年6月29日 07:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定