第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用PowerShell循環(huán)瀏覽目錄中的文件

使用PowerShell循環(huán)瀏覽目錄中的文件

如何更改以下代碼以查看目錄中的所有.log文件,而不僅僅是一個(gè)文件?我需要遍歷所有文件并刪除所有不包含“ step4”或“ step9”的行。當(dāng)前,這將創(chuàng)建一個(gè)新文件,但是我不確定如何在for each此處使用循環(huán)(新手)。實(shí)際文件的命名如下: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貢獻(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')

}


查看完整回答
反對(duì) 回復(fù) 2019-11-25
?
Smart貓小萌

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

}


查看完整回答
反對(duì) 回復(fù) 2019-11-25
?
開滿天機(jī)

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ù)邏輯。


查看完整回答
反對(duì) 回復(fù) 2019-11-25
  • 3 回答
  • 0 關(guān)注
  • 1109 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)