我已將以下 Powershell 腳本放在一起以執行以下操作:
- 將所有子檔案夾中的所有檔案移動到檔案夾的根目錄中
- 洗掉所有子檔案夾
- 最后,將所有檔案移至根目錄后,洗掉所有 json 檔案
我的PowerShell腳本:
Get-ChildItem -Path ./ -Recurse -File | Move-Item -Destination ./ ; Get-ChildItem -Path ./ -Recurse -Directory | Remove-Item;
Get-ChildItem *.json | foreach { Remove-Item -Path $_.FullName }
我必須使用 cd 來更改每個檔案夾的作業目錄,如何傳遞檔案夾名稱并為每個檔案夾執行上述腳本。
--> So there is a parent folder
--> There are 100s of Sub-Folders in the parent Folder
--> Each Sub-folder have many sub-folders and many files
如何傳遞父檔案夾名稱,以便它遍歷父檔案夾中的每個子檔案夾并為每個子檔案夾獨立運行 powershell 腳本?
ParentFolder
Child-Sub-Folder1 <-- Move all files in the sub-folders to the root folder Child-Sub-Folder1
Child-Sub-Folder2 <-- Move all files in the sub-folders to the root folder Child-Sub-Folder2
Child-Sub-Folder3 <-- Move all files in the sub-folders to the root folder Child-Sub-Folder3
uj5u.com熱心網友回復:
將行程放在 Foreach-Object 塊中。這使您可以定位父目錄并處理其中的每個子檔案夾。這不考慮像@Theo 評論的檔案名沖突。
$parentFolder = "PathToParent"
Get-ChildItem -Path $parentFolder | ForEach-Object {
$subFolder = $_.FullName
Get-ChildItem -Path $subFolder -Recurse -File | ForEach-Object {
#Move-Item $_.FullName -Destination $subFolder
}
Get-ChildItem -Path $subFolder -Recurse -Directory | Remove-Item
Get-ChildItem -Path $subFolder "*.json" | ForEach-Object {
Remove-Item -Path $_.FullName
}
}
uj5u.com熱心網友回復:
這就是我最終做的:
$dir = dir "D:\MyFolder\" | ?{$_.PSISContainer}
foreach ($d in $dir){
$name = $d.FullName
$name
Get-ChildItem -Path $name -Recurse -File | Move-Item -Destination $name ; Get-ChildItem -Path $name -Recurse -Directory | Remove-Item -Confirm:$false
Get-ChildItem -Path $name *.json | foreach { Remove-Item -Path $_.FullName } -Confirm:$false
Get-ChildItem -Path $name *.heic | foreach { Remove-Item -Path $_.FullName } -Confirm:$false
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375528.html
