powershell - 连接 N 个文本文件并将文件名添加到每一行 [英] powershell - concatenate N text files and prepend filename to each line
本文介绍了powershell - 连接 N 个文本文件并将文件名添加到每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Powershell - 简单的问题.
Powershell - easy question.
我想连接 N 个文本文件并将文件名添加到每一行.如何做同样但有完整路径+文件名?每个文件都应按原样保留所有文本和行终止符.
I want to concatenate N text files and prepend filename to each line. How to do same but have full path+filename? Each files should retain all text and line terminators as-is.
data1.txt
this is the text in
data1 file - hello
data2.txt
four score and
seven years ago
获取这个文件
输出.txt
data1.txt: this is the text in
data1.txt: data1 file - hello
data2.txt: four score and
data2.txt: seven years ago
它会是这样的 - 但它确实有效.
It will be something like this - but that actually works.
get-content c:\temp\data*.txt|foreach-object{$tmp=get-content $;$.basename+","+$tmp} > output.txt
get-content c:\temp\data*.txt|foreach-object{$tmp=get-content $;$.basename+","+$tmp} > output.txt
谢谢
推荐答案
试试这个:
$path = "c:\temp"
$out = "c:\temp\output.txt"
Get-ChildItem $path -Filter data*.txt | % {
$file = $_.Name
Get-Content $_.FullName | % {
"${file}: $_" | Out-File -Append $out
}
}
这篇关于powershell - 连接 N 个文本文件并将文件名添加到每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文