2017-04-10 155 views
1

我有大約50個文件,我將它們合併成一個文件名,然後是該文件的內容,然後在文件輸出後留下一行可能是虛線例如它應該是這樣的如何使用PowerShell中的文件內容獲取文件名

 
File name -ABC 
xxxxxxxxxxxxxxxx (Content of the file) 
..................... (dotted line after output) 
File Name - CDE 
xxxxxxxxxxxx (Content of the file) 
................... 
Get-ChildItem C:\temp | Get-Content 

這個腳本給我我想要的格式輸出不不。我無法找到獲取文件名稱的方法。

回答

3

你想要的是相當平凡的。您只需要一個ForEach-Object循環來分別處理每個輸入文件,並使用格式運算符(-f)將您的數據注入到模板字符串中:

Get-ChildItem 'C:\temp' | ForEach-Object { 
    @' 
File name - {0} 
{1} 
..................... 
'@ -f $_.Name, (Get-Content $_.FullName -Raw) 
} | Out-File 'C:\path\to\output.txt' 
相關問題