3 回答

TA貢獻1880條經驗 獲得超4個贊
試試看:
Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log |
Foreach-Object {
$content = Get-Content $_.FullName
#filter and save content to the original file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save content to a new file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content ($_.BaseName + '_out.log')
}

TA貢獻1911條經驗 獲得超7個贊
要獲取目錄的內容,可以使用
$files = Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files\"
然后,您也可以遍歷此變量:
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
Get-Content $files[$i].FullName | Where-Object { ($_ -match 'step4' -or $_ -match 'step9') } | Set-Content $outfile
}
放置foreach循環的一種更簡單的方法是循環(感謝@Soapy和@MarkSchultheiss):
foreach ($f in $files){
$outfile = $f.FullName + "out"
Get-Content $f.FullName | Where-Object { ($_ -match 'step4' -or $_ -match 'step9') } | Set-Content $outfile
}

TA貢獻1786條經驗 獲得超13個贊
如果您需要遞歸地在目錄中循環查找特定類型的文件,請使用以下命令,該命令將過濾所有doc
文件類型的文件
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc
如果需要對多種類型進行過濾,請使用以下命令。
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc,*.pdf
現在,$fileNames
變量充當一個數組,您可以從中循環并應用業務邏輯。
添加回答
舉報