如何使用PowerShell中的文件内容获取文件名
问题描述:
我有大约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
这个脚本给我我想要的格式输出不不。我无法找到获取文件名称的方法。
答
你想要的是相当平凡的。您只需要一个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'