首页 > 解决方案 > 如何在没有中间文件的情况下将文件附加到目标文件的开头?

问题描述

我想在没有中间文件的情况下将文件 toppart_ABC.txt(包含'ABC')附加到destinationfile_DEF.txt(包含'DEF')的开头。

如果我尝试

Add-Content -Path .\destinationfile_DEF.txt -Value (Get-Content .\toppart_ABC.txt)

这会更改正确的文件destinationfile_DEF.txt,但内容错误-它将文本放在底部,即defabc

如果我尝试

Add-Content -Path .\toppart_ABC.txt -Value (Get-Content .\destinationfile_DEF.txt)

这会更改错误的文件 toppart_ABC.txt,但具有正确的内容,即 abcdef

理想情况下寻找一个班轮,但不是至关重要的

标签: powershellmergeappend

解决方案


如果您不反对只重写文件,则可以Set-Content在检索两个文件内容后执行:

Set-Content -Path .\destinationfile_DEF.txt -Value (Get-Content .\toppart_ABC.txt,.\destinationfile_DEF.txt)

推荐阅读