Powershell将文本添加到输出文件的开始、结束
我有一个包含多个文本文件的文件夹,我正在将这些文件合并成一个文件。在这个输出文件上,我需要在文件的顶部和底部添加一个字符串。我尝试过使用insert,但一直出现错误。该文件包含在$Output变量中 到目前为止,我的代码是:Powershell将文本添加到输出文件的开始、结束,powershell,parsing,Powershell,Parsing,我有一个包含多个文本文件的文件夹,我正在将这些文件合并成一个文件。在这个输出文件上,我需要在文件的顶部和底部添加一个字符串。我尝试过使用insert,但一直出现错误。该文件包含在$Output变量中 到目前为止,我的代码是: if(!(Test-Path -Path $PathDump)) { # create the folder if it does not yet exist New-Item -ItemType Directory $PathDump } # move a
if(!(Test-Path -Path $PathDump)) {
# create the folder if it does not yet exist
New-Item -ItemType Directory $PathDump
}
# move all *.txt items from 'C:\RemoveFirst\txt' to 'C:\RemoveFirst\DumpARoo'
# EXCEPT the output file itself
$Path = (Get-ChildItem -Path $Path -Filter '*.txt' -File).FullName | Where-Object { $_ -ne $Output}
Move-Item -Path $Path -Destination $PathDump # move (not copy) files into new directory to concat
Get-ChildItem -Path $PathDump -Filter '*.txt' -File | ForEach-Object {
'' # Output an empty line at SOF
'---------------------------------------------------------------'
$_ | Get-Content | Select-Object -Skip 1 | Select-Object -SkipLast 1
'---------------------------------------------------------------'
'' # Output an empty line at EOF
} | Add-Content -Path $OutPut
故事还在继续;)
试试这个:
$Path = 'C:\RemoveFirst\*.txt'
$PathDump = 'C:\RemoveFirst\DumpARoo'
$Output = 'C:\RemoveFirst\TestingFile.txt'
if(!(Test-Path -Path $PathDump)) {
# create the folder if it does not yet exist
New-Item -ItemType Directory $PathDump
}
# move all *.txt items from 'C:\RemoveFirst\txt' to 'C:\RemoveFirst\DumpARoo'
# EXCEPT the output file itself
$Path = (Get-ChildItem -Path $Path -Filter '*.txt' -File).FullName | Where-Object { $_ -ne $Output}
Move-Item -Path $Path -Destination $PathDump # move (not copy) files into new directory to concat
# Output 'SOF'
"SOF" | Add-Content -Path $OutPut
Get-ChildItem -Path $PathDump -Filter '*.txt' -File | ForEach-Object {
# output the content of the current file
$_ | Get-Content | Select-Object -Skip 1 | Select-Object -SkipLast 1 | Add-Content -Path $OutPut
}
# Output 'EOF'
"EOF" | Add-Content -Path $OutPut
请尝试接受你的老问题的答案,如果它们对你有帮助的话。对不起!我修复了itRight现在我在每个合并到输出中的文本文件的开头和结尾处都有一条虚线,但在实际的输出文件中,我需要SOF在开头,EOF在结尾end@GarrettStarkey好的,如果不需要虚线,只需添加内容-Path$OutPut,然后添加内容,最后添加
“EOF“|添加内容-路径$OutPut
。你可以随心所欲地创作。我现在明白了,我完全看错了!这就是这一系列:)谢谢,实际上它不起作用……我不想让它对每个单独的文件说SOF和EOF,我想让它对一个大文件说,当所有的都说了和done@GarrettStarkey为了清楚起见,我在回答中改变了这一点。