簡體   English   中英

Powershell發出文件外

[英]Powershell issues out-file

我正在嘗試在文本文件中搜索某些文本,然后僅在匹配時將該文本輸出到其他文本文件。 我在創建此文件時遇到了麻煩。 我附上了我擁有的代碼,但是問題不在於它創建了一個results.txt文件,而是文件為空。 我僅希望在multiplatform_201604110718.txt中包含CCTK狀態代碼:SUCCESS的情況下創建result.txt文件。

$Path = "C:\multiplatform_201604110718.txt"
$Text = "CCTK STATUS CODE : SUCCESS"
$PathArray = @()
$Results = "C:\results.txt"

# This code snippet gets all the files in $Path that end in “.txt”.
Get-ChildItem $Path -Filter “*.txt” |
Where-Object { $_.Attributes -ne “Directory”} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) { 
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}
Write-Host “Contents of ArrayPath:”
$PathArray | ForEach-Object {$_}
$PathArray | % {$_} | Out-File "C:\Resuts.txt"

如果您真的只需要檢查一個文件,則可以用更少的代碼來完成:

$Text = "CCTK STATUS CODE : SUCCESS"
$p = "C:\Users\kzbx\Desktop\test.txt"

if($ln = Select-String -pattern $text -path $p){
$ln.Line | Out-File C:\result.txt
}

這會將文本出現的行寫到結果文件中(如果我正確理解了那就是您所需要的,如果不需要,請澄清;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM