3 回答

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
試試看:
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貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
要獲取目錄的內(nèi)容,可以使用
$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循環(huán)的一種更簡單的方法是循環(huán)(感謝@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貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果您需要遞歸地在目錄中循環(huán)查找特定類型的文件,請(qǐng)使用以下命令,該命令將過濾所有doc
文件類型的文件
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc
如果需要對(duì)多種類型進(jìn)行過濾,請(qǐng)使用以下命令。
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc,*.pdf
現(xiàn)在,$fileNames
變量充當(dāng)一個(gè)數(shù)組,您可以從中循環(huán)并應(yīng)用業(yè)務(wù)邏輯。
添加回答
舉報(bào)