如何使用 PowerShell 拼接文件
-
使用
Get-Content
和Set-Content
在 PowerShell 中连接文件 -
使用
Add-Content
在 PowerShell 中连接文件 -
使用
Out-File
在 PowerShell 中连接文件 - 使用连接运算符在 PowerShell 中连接文件
- 结论

PowerShell 允许您执行不同的文件操作,例如创建、复制、移动、删除、查看和重命名文件。另一个重要功能是可以将多个文件的内容连接成一个文件。
在本文中,我们将探讨四种方法,轻松将多个文件的内容合并为一个文件:使用 Get-Content
和 Set-Content
,使用 Add-Content
cmdlet,使用 Out-File
cmdlet,以及使用连接运算符。
使用 Get-Content
和 Set-Content
在 PowerShell 中连接文件
此方法利用 Get-Content
读取多个文件(file1.txt
和 file2.txt
)的内容,然后将合并的内容通过管道传递给 Set-Content
,以将其写入新文件(concatenated.txt
)。
Set-Content
是 PowerShell 中的一个字符串处理 cmdlet。它写入新内容或替换文件中的内容。
file1.txt
:
This is a file1 text file.
file2.txt
:
This is a file2 text file.
代码:
Get-Content file1.txt, file2.txt | Set-Content concatenated.txt
在此命令中,Get-Content file1.txt, file2.txt
读取 file1.txt
和 file2.txt
的内容。
然后,当我们看到垂直线 |
时,这是管道运算符,它将输出(file1.txt
和 file2.txt
的内容)传递给命令的下一部分。
最后,Set-Content concatenated.txt
命令将连接文件的内容设置到名为 concatenated.txt
的新文件中。
使用 Get-Content
检查 concatenated.txt
文件的内容。使用下面的命令示例。
Get-Content concatenated.txt
Get-Content
命令读取指定文件的内容并将其输出到控制台。如果 concatenated.txt
在当前目录中,该命令将显示其内容。
如果文件不在当前目录,请确保将 concatenated.txt
替换为您文件的实际路径。
我们还将在接下来讨论的方法中使用 Get-Content
来检查和验证文件内容是否正确。
输出:
This is a file1 text file.
This is a file2 text file.
它将显示两个文件的合并内容,并自动在每个文件之间添加新行。
使用 Add-Content
在 PowerShell 中连接文件
在这里,Get-Content
用于单独读取每个文件的内容。然后,Add-Content
cmdlet 将内容附加到指定文件(concatenated2.txt
)。这个过程对每个文件重复。
file1.txt
:
This is a file1 text file.
file2.txt
:
This is a file2 text file.
代码:
Get-Content file1.txt | Add-Content -Path concatenated2.txt
Get-Content file2.txt | Add-Content -Path concatenated2.txt
此命令读取 file1.txt
和 file2.txt
的内容,并将它们附加到现有的 concatenated2.txt
文件末尾。
|
(管道)运算符将两个文件的合并内容传递给 Add-Content
cmdlet。
注意:确保文件路径正确,并且您具有从
file1.txt
和file2.txt
读取及写入concatenated2.txt
的必要权限。
使用 Get-Content
检查 concatenated2.txt
文件的内容。
Get-Content concatenated2.txt
输出:
This is a file1 text file.
This is a file2 text file.
它将显示两个文件的合并内容,并自动在每个文件之间添加新行。
使用 Out-File
在 PowerShell 中连接文件
Out-File
cmdlet 将输出发送到文件。如果文件不存在,则在指定路径创建新文件。
要使用 Out-File
连接文件,您需要使用 Get-Content
cmdlet 获取文件的内容。通常,Get-Content
在控制台中显示输出。
您必须将它的输出通过管道传递给 Out-File
,以便将输出发送到指定的文件。以下示例将两个文件的内容合并到 concatenated3.txt
中。
file1.txt
:
This is a file1 text file.
file2.txt
:
This is a file2 text file.
代码:
Get-Content file1.txt, file2.txt | Out-File concatenated3.txt
上面的命令将用 file1.txt
和 file2.txt
的合并内容覆盖 concatenated3.txt
的内容。如果 concatenated3.txt
不存在,将创建它。
与前一种方法相比,Add-Content
附加现有文件,而 Out-File
则覆盖它。
运行以下命令以验证 concatenated3.txt
文件的内容。
Get-Content concatenated3.txt
输出:
This is a test1 file.
This is a test2 file.
如您所见,test1.txt
和 test2.txt
的内容被复制到 concatenated3.txt
中。
如果您想连接目录中的所有 .txt
文件,可以使用 *.txt
选择目录中所有扩展名为 .txt
的文件。
Get-Content *.txt | Out-File concatenated3.txt
使用连接运算符在 PowerShell 中连接文件
在此方法中,+
运算符连接 file1.txt
和 file2.txt
的内容,然后将其传递给 Set-Content
以写入目标文件(concatenated4.txt
)。
file1.txt
:
This is a file1 text file.
file2.txt
:
This is a file2 text file.
代码:
(Get-Content file1.txt -Raw) + "`r`n" + (Get-Content file2.txt -Raw) | Set-Content concatenated4.txt
+ "`r`n" +
部分用于连接字符串。
在这种情况下,它在 file1.txt
和 file2.txt
的内容之间添加一个回车符 ("r")
和一个换行符 ("n")
。这确保在合并时两个文件的内容将位于不同的行上。
使用 Get-Content
检查 concatenated4.txt
文件的内容。
Get-Content concatenated4.txt
输出:
This is a file1 text file.
This is a file2 text file.
它将显示两个文件的合并内容,并支持 + "`r`n" +
在每个文件之间添加新行。
结论
在 PowerShell 中连接文件是一项多功能的任务,选择方法取决于您的特定需求。
无论您是更喜欢 Get-Content
和 Set-Content
管道的简便性,Add-Content
与 Out-File
的明确性,还是连接运算符的简洁语法,PowerShell 提供了多种选项以满足您的需求。
理解这些方法使您能够在各种情况下有效地操纵和管理文件内容。