如何使用PowerShell为多个子目录创建目录连接?

时间:2013-11-04 17:58:27

标签: windows function powershell scripting mklink

有很多子文件夹,只能为二阶子文件夹创建目录连接。

当前文件夹结构的某些部分如下所示

C:\MainFolder>tree
Folder PATH listing for volume C System
C:.
├───1stSub-FolderA
│   ├───2ndSub-FolderA
│   └───2ndSub-FolderB
└───1stSub-FolderB
    ├───2ndSub-FolderA
    └───2ndSub-FolderB

D:\MainFolder-Copy>tree
Folder PATH listing for volume Dosya
D:.
├───1stSub-FolderA
└───1stSub-FolderB

每个文件夹的手动编码:

C:\>mklink /j D:\MainFolder-Copy\1stSub-FolderA\2ndSub-FolderA C:\MainFolder\1stSub-FolderA\2ndSub-FolderA
Junction created for D:\MainFolder-Copy\1stSub-FolderA\2ndSub-FolderA <<===>> C:\MainFolder\1stSub-FolderA\2ndSub-FolderA

C:\>mklink /j D:\MainFolder-Copy\1stSub-FolderA\2ndSub-FolderB C:\MainFolder\1stSub-FolderA\2ndSub-FolderB
Junction created for D:\MainFolder-Copy\1stSub-FolderA\2ndSub-FolderB <<===>> C:\MainFolder\1stSub-FolderA\2ndSub-FolderB

C:\>mklink /j D:\MainFolder-Copy\1stSub-FolderB\2ndSub-FolderA C:\MainFolder\1stSub-FolderB\2ndSub-FolderA
Junction created for D:\MainFolder-Copy\1stSub-FolderB\2ndSub-FolderA <<===>> C:\MainFolder\1stSub-FolderB\2ndSub-FolderA

C:\>mklink /j D:\MainFolder-Copy\1stSub-FolderB\2ndSub-FolderB C:\MainFolder\1stSub-FolderB\2ndSub-FolderB
Junction created for D:\MainFolder-Copy\1stSub-FolderB\2ndSub-FolderB <<===>> C:\MainFolder\1stSub-FolderB\2ndSub-FolderB

结果:

C:\MainFolder>tree
Folder PATH listing for volume C System
C:.
├───1stSub-FolderA
│   ├───2ndSub-FolderA
│   └───2ndSub-FolderB
└───1stSub-FolderB
    ├───2ndSub-FolderA
    └───2ndSub-FolderB

D:\MainFolder-Copy>tree
Folder PATH listing for volume Dosya
D:.
├───1stSub-FolderA
│   ├───2ndSub-FolderA
│   └───2ndSub-FolderB
└───1stSub-FolderB
    ├───2ndSub-FolderA
    └───2ndSub-FolderB

如何使用几个命令为所有仅仅2个子文件夹创建它而不是为每个文件夹手动编写代码?命令不应为第1,第3,第4,第5 ...订单子文件夹创建目录连接。

1 个答案:

答案 0 :(得分:4)

试试这个:

Get-ChildItem D:\MainFolder-Copy -Directory |
    Foreach {Get-ChildItem (Join-Path C:\MainFolder $_.Name) -Directory} | 
    Foreach {cmd /c mklink /j ($_.FullName -replace 'C:\\MainFolder','D:\MainFolder-Copy') $_.FullName}