2016-11-14 115 views
0

正如您在下面看到的,將提取文件夾內所有.msg文件的內容。但是,我只能在輸出content.txt中看到1個.msg內容,而不是所有的.msg文件。我如何循環命令並添加到相同的.txt文件?將輸出數據添加到1個文本文件

$output_file3 = ‘c:\outlook_files\content.txt’ 

# Search messages 
Get-ChildItem "c:\outlook_files\msg\" -Filter *.msg | Foreach-Object { 
    # Read current message content 
    $content = Get-Content $_.FullName >$output_file3 
} 
+1

'獲取幫助'about_Redirection'-ShowWindow' – JosefZ

回答

1

而不是使用重定向,使用Out-File -appendAdd-Content

所以更改

$content = Get-Content $_.FullName >$output_file3

Get-Content $_.FullName | out-file $output_file3 -append

我放棄了變量賦值,因爲它在這個上下文中沒有做任何事情。

1

>redirection operator截斷現有文件。無論是使用追加操作:

Get-Content $_.FullName >>$output_file3 
Get-Content $_.FullName | Add-Content $output_file3 
Get-Content $_.FullName | Out-File $output_file3 -Append 

ForEach-Object環後,應進行寫操作:

ForEach-Object { 
    Get-Content $_.FullName 
} >$output_file3 

ForEach-Object { 
    Get-Content $_.FullName 
} | Set-Content $output_file3 

ForEach-Object { 
    Get-Content $_.FullName 
} | Out-File $output_file3 

通常你會喜歡*-ContentOut-File的cmdlet超過重定向操作符,因爲他們給你更好的控制關於寫入文件的內容(即編碼)。 Out-FileSet-Content/Add-Content之間的主要區別在於前者默認爲Unicode(小尾數UTF-16是精確的),而後者默認爲Ascii(實際上是windows-1252 ANSI編碼)。

0

如果你不想白白

Get-Content "c:\outlook_files\msg\\*.msg" | Out-File "c:\outlook_files\content.txt" 

Get-Content "c:\outlook_files\msg\\*.msg" > "c:\outlook_files\content.txt" 
0

您可以使用添加內容,像這個例子循環,可以簡單地這樣做:

add-content -path "c:\test.txt" -value "$value"