使用Windows命令行重命名文件夹

时间:2014-01-30 16:10:52

标签: powershell command-line windows-7

我有两组深层文件夹,例如:

Folder A
    Sub Folder A – 1
        <Files>
    Sub Folder A – 2
        <Files>
Folder B
    Sub Folder B – 1
        <Files>
Folder C
    Sub Folder C – 1
        <Files>
    Sub Folder C – 2
        <Files>
    Sub Folder C – 3
        <Files>

我需要使用Windows 7 CommandLine或Powershell重命名子文件夹,以便在其中包含父文件夹的名称,例如

Sub Folder A - 1 

将重命名为

Folder A - Sub Folder A - 1  

可能?

由于

2 个答案:

答案 0 :(得分:2)

此代码应该为您完成。

$RootDirectory = 'c:\test';

# 1. Get list of parent folders
$ParentFolderList = Get-ChildItem -Path $RootDirectory -Directory;

# 2. Iterate over top-level folders
foreach ($ParentFolder in $ParentFolderList) {
    $ChildFolderList = Get-ChildItem -Path $ParentFolder.FullName

    # 3. Iterate over subfolders of top-level folders, and rename them
    foreach ($ChildFolder in $ChildFolderList) {
        $NewFolderName = '{0}\{1} - {2}' -f $ParentFolder.FullName, $ParentFolder.Name, $ChildFolder.Name;
        Rename-Item -Path $ChildFolder.FullName -NewName $NewFolderName -WhatIf;
    }
}

-WhatIf cmdlet中删除Rename-Item参数,让其进行更改。

答案 1 :(得分:1)

您可以在命令提示符下将其作为bat运行

for /d %%i in (*) do (
     for /d %%x in ("%%i/*") do ren "%%i/%%x" "%%i - %%x"
)