2013-11-04 91 views
1

有很多子文件夾,並且只能爲二階子文件夾創建目錄連接。如何使用PowerShell爲多個子目錄創建目錄連接?

當前文件夾結構的某些部分如下所示

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 

如何使它對一切都只是第二訂購子文件夾幾個命令,而不是手動爲每個文件夾寫代碼?命令不應該爲第1,第3,第4,第5 ...個子文件夾創建目錄結點。

回答

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} 
+0

它應該有「CMD/C」 mklink之前,因爲「術語‘mklink’不被識別爲cmdlet,函數,腳本文件或可操作 程序的名稱。 「 – ide

+0

是的,mklink是內置於cmd.exe中的,並不是一個exe文件。謝謝。 –