2016-06-10 9 views
1

我一直在試圖得到一個非常具體的目錄列表,迄今爲止沒有成功。兩個級別的目錄列表與Powershell

可以說我有一個文件夾結構是這樣的:

  • C:\的Inetpub \更新\ updatefolder1
  • C:\的Inetpub \更新\ updatefolder1 \網絡\
  • C:\的Inetpub \更新\ updatefolder1 \網絡\(站點代碼,結構變亮)
  • C:\的Inetpub \更新\ updatefolder1 \腳本\(DB腳本)
  • C:\的Inetpub \更新\ updatefolder2
  • C:\的Inetpub \更新\ updatefolder2 \網絡\(站點代碼,結構變亮)

現在,當我使用的foreach,它看起來就像這樣:

$folders = Get-ChildItem $UpdateDir -Directory -Recurse -Depth 1 | Select-Object FullName 

ForEach($folder in $folders) { 
    $folder = folder.TrimStart("@{FullName=").TrimEnd("}") #To get a clean name 
} 

結果將如下:

C:\inetpub\updates\updatefolder1 
C:\inetpub\updates\updatefolder2 
C:\inetpub\updates\updatefolder1\Web\ 
C:\inetpub\updates\updatefolder1\Web\ (site code, the structure goes on) 
C:\inetpub\updates\updatefolder1\Scripts\ (db scripts) 
C:\inetpub\updates\updatefolder2\Web\ (site code, the structure goes on) 

哪個不好。 它應該看起來像這樣:

C:\inetpub\updates\updatefolder1 
C:\inetpub\updates\updatefolder1\Web\ 
C:\inetpub\updates\updatefolder1\Web\ (site code, the structure goes on) 
C:\inetpub\updates\updatefolder1\Scripts\ (db scripts) 
C:\inetpub\updates\updatefolder2 
C:\inetpub\updates\updatefolder2\Web\ (site code, the structure goes on) 

任何新穎的想法?我嘗試了幾件事,除了那個foreach循環之外,仍然沒有運氣。

在此先感謝。

+0

1.你已經張貼在這裏似乎有許多錯別字的代碼的結果進行排序。 2.你想達到什麼目的? 3.您發佈的兩個結果似乎都是相同的,只是序列/順序不同。你可以請檢查一次並確認嗎? – SavindraSingh

+0

問題是你用來獲取全名的可怕黑客攻擊。刪除整個'ForEach'循環並將第一行更改爲'$ folders = Get-ChildItem $ UpdateDir -Directory -Recurse -Depth 1 | Select-Object -Expand FullName' – TheMadTechnician

+0

@SavindraSingh錯別字?哪裏?除了我忘記添加變量實際輸出的事實。 – Ramil

回答

0

ForEach循環不需要,它可以在選擇對象中替換爲-expandproperty。如果我正確地理解了這個問題,那麼你提出的問題是結果沒有排序。答案是使用`排序-對象」

$folders = Get-ChildItem $UpdateDir -Directory -Recurse -Depth 1 | 
    Select-Object -ExpandProperty fullname | 
    Sort-Object 
$folders 
+0

謝謝!這正是我想要的。對於糟糕的解釋抱歉,我勉強解釋一些東西,我會成爲最難懂的老師。這也是我添加視覺表現的原因。 我試圖破解它一個星期,解決方案非常簡單,該死的。 – Ramil