
我無法根據條件返回(許多)嵌套資料夾的列表,並且在文件和網站(例如本文檔)中在線搜索時嘗試了許多不同的方法。
我正在嘗試以下行:
gci -recur -dir | ? { !(gci $_ -file -recur -filter √.txt) -and (gci $_ -file -recur -filter *.mp3) } | select name
並期望返回滿足以下兩個條件的資料夾清單:至少包含一個 .mp3 檔案作為直接子代,但不包含名為「√.txt」的檔案作為直接子代,即:
-music
--artist
---album
----song1.mp3 將只返回專輯目錄名稱或路徑,而:
-music
--artist
---album
----√.txt
----song1。
將被跳過,重複播放大約10,000 個不同嵌套的專輯。
更簡單地說,我希望以下程式碼返回包含(作為直接子級)1 個或多個 .mp3 檔案的子目錄列表,但事實並非如此:
gci -recur -dir | ? { (gci $_ -file -recur -filter *.mp3) } | select name
儘管呼叫了 -recur,它僅返回當前目錄的直接子目錄(例如“rock”或“jazz”等類別資料夾)。
我想要發生的是我搜尋一個資料夾“music”,其中有數千個嵌套子目錄,並返回子目錄清單(無論如何嵌套),其中同時包含(作為直接子層級)mp3但不包含“√ .txt”(作為直接子級)。
希望這有足夠的意義!如果需要更多資訊或澄清,請告訴我。
這是資料夾結構的片段:
目錄:D:\music\rock\90s\Stereolab\albums:
1992 - 彭!
1992 年 - 開啟
1994 年 - 火星奧迪克五重奏
謝謝你!
答案1
僅用一個命令是不可能做到這一點的。您要做的就是建立一個包含至少一個 mp3 的所有資料夾的列表,然後僅使用該列表執行第二次循環,看看該資料夾是否包含您的檔案。如果是這樣,請從清單中刪除該條目。
這是一個範例腳本:
$targetFolder = "C:\Test"
# Step 1: Scan the target folder and its subfolders for MP3 files
$mp3Files = Get-ChildItem -Path $targetFolder -Filter "*.mp3" -Recurse `
| Select-Object -ExpandProperty Directory | Get-Unique
# Step 2: Filter the list to exclude folders containing 'v.txt'
$filteredFolders = $mp3Files | Where-Object {
$vTxtFile = Get-ChildItem -Path $_ -Filter "v.txt" -File `
-ErrorAction SilentlyContinue
-not $vTxtFile
}
# Step 3: Output the resulting folders
$filteredFolders