我所在的資料夾有 24 個子資料夾。子資料夾名為 Angle1、Angle2、...、Angle24。在每個子資料夾中,我想執行以下命令:
Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt
有沒有辦法使用 Windows PowerShell 來執行此操作?
請注意,我不想在子子資料夾中執行命令。
這並沒有完全做到我想要的:
Get-ChildItem/*/ PKA.dump -Recurse | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt
答案1
你可以使用這個:
在父級中建立一個 PowerShell 物件(即script.ps1
持有Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt
),並透過以下方式啟用未簽名執行:Set-ExecutionPolicy Unrestricted
從管理 PowerShell
將其複製並貼上到批次文件中:
copy script.ps1 Angle1
cd Angle1
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle2
cd Angle2
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle3
cd Angle3
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle4
cd Angle4
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle5
cd Angle5
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle6
cd Angle6
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle7
cd Angle7
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle8
cd Angle8
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle9
cd Angle9
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle10
cd Angle10
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle11
cd Angle11
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle12
cd Angle12
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle13
cd Angle13
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle14
cd Angle14
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle15
cd Angle15
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle16
cd Angle16
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle17
cd Angle17
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle18
cd Angle18
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle19
cd Angle19
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle20
cd Angle20
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle21
cd Angle21
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle22
cd Angle22
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle23
cd Angle23
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle24
cd Angle24
powershell .\script.ps1
del script.ps1
cd ..
exit
儲存並以管理員身份執行該腳本。
例如:script.bat
右鍵單擊,Run as Administrator