亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用PowerShell循環瀏覽目錄中的文件

使用PowerShell循環瀏覽目錄中的文件

如何更改以下代碼以查看目錄中的所有.log文件,而不僅僅是一個文件?我需要遍歷所有文件并刪除所有不包含“ step4”或“ step9”的行。當前,這將創建一個新文件,但是我不確定如何在for each此處使用循環(新手)。實際文件的命名如下:2013 09 03 00_01_29.log。我希望輸出文件覆蓋它們,或者具有相同的名稱,并附加“ out”。$In = "C:\Users\gerhardl\Documents\My Received Files\Test_In.log"$Out = "C:\Users\gerhardl\Documents\My Received Files\Test_Out.log"$Files = "C:\Users\gerhardl\Documents\My Received Files\"Get-Content $In | Where-Object {$_ -match 'step4' -or $_ -match 'step9'} | `Set-Content $Out
查看完整描述

3 回答

?
慕村225694

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')

}


查看完整回答
反對 回復 2019-11-25
?
Smart貓小萌

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

}


查看完整回答
反對 回復 2019-11-25
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

如果您需要遞歸地在目錄中循環查找特定類型的文件,請使用以下命令,該命令將過濾所有doc文件類型的文件

$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc

如果需要對多種類型進行過濾,請使用以下命令。

$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc,*.pdf

現在,$fileNames變量充當一個數組,您可以從中循環并應用業務邏輯。


查看完整回答
反對 回復 2019-11-25
  • 3 回答
  • 0 關注
  • 1110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號