当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 将数据附加到文件 [ 4 种方法]

作者:精品下载站 日期:2024-12-14 05:29:57 浏览:15 分类:玩电脑

使用 PowerShell 将数据附加到文件 [ 4 种方法]


[玩转系统] 使用 PowerShell 将数据附加到文件 [ 4 种方法]

将文本附加到文本文件

我们有多种方法使用 PowerShell 在文本文件中附加内容。例如,我们可以在同一行、带/不带特殊字符的新行、多行上附加信息,以及附加格式化数据(例如表格内容)。但是,在转向可能的解决方案之前,必须了解我们将在本节下面的每种方法中使用的文件和文件内容。 file.txt 位于 E:\Test 目录中。

file.txt文件的内容:

This is the first line.

请注意,我们的光标位于同一行,就在句点 (.) 之后,这将帮助我们在同一行上追加内容。让我们使用这个文件来探索下面的各种解决方案。

使用 Out-File Cmdlet

使用 Out-File cmdlet 和 -Append 参数将内容附加到 file.txt 文件中的同一行。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.

使用 Out-File cmdlet 和 -Append 参数将内容附加到 file.txt 文件的新行上。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.

你发现上面两个例子有什么相似之处吗?是的!为什么会这样呢?因为在第一个示例中第一次使用了 -Append ,所以它将内容附加到与光标位于同一行的同一行上,但也在文件末尾添加了一个新行字符。现在,光标已换行;这就是为什么第二个代码示例中的 -Append 在新行中添加了内容,但它还在文件末尾添加了一个新行字符。请注意,-Append 参数会自动在文件末尾添加换行符;这就是为什么除了第一个示例之外每次都会将内容附加到新行的原因。

使用带有 -Append 参数的 Out-File cmdlet 在 file.txt中附加带有特殊字符(例如制表符)的内容> 文件。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

使用 Out-File cmdlet 和 -Append 参数在 file.txt 文件中追加多行。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

使用 Out-File cmdlet 和 -Append 参数在 file.txt 文件中附加表格内容(格式化内容)。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

使用 Out-File cmdlet 在 file.txt 文件中附加包含单引号和双引号的行。

使用输出文件 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double quotes`"."
Out-File -FilePath $filePath -InputObject $content -Append
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the eighth line having 'single quote' and "double quotes".

Out-File cmdlet 用于将输出发送到指定文件。此 cmdlet 隐式使用 PowerShell 的格式化系统写入文件。请注意,此文件将获得与终端上完全相同的显示表示形式,这意味着如果输出中涉及的每个输入对象都是字符串,则输出可能足以进行编程处理。我们使用此 cmdlet 将输出 ($content) 发送到 file.txt 文件。

在上面的示例中,我们创建了两个名为 $filePath$content 的变量。 $filePath 包含 file.txt 文件的路径,而 $content 包含我们要附加的实际内容。 Get-Content cmdlet 用于检索 file.txt 文件的全部数据(内容)并将其显示在终端上,这有助于查看更新的内容无需打开 file.txt 文件。

唯一的区别是我们如何根据需求使用 Out-File cmdlet。所有示例中的 -FilePath-InputObject-Append 参数均相同。那么这些参数的含义是什么,我们为什么要使用它们呢?

-FilePath 用于指定 $filePath-InputObject 用于提及我们想要的 $content追加,-Append 用于将 $content 追加到 file.txt 文件的末尾。 您还可以使用 -Encoding 参数指定目标文件的编码类型。通常,使用 UTF8 编码,而 utf8NoBOM 为其默认值。我们还有其他编码,可以根据我们的要求使用。

现在,重点是内容的格式如何。这是在初始化每个示例的 $content 变量时完成的。我们使用简单的文本在第一个和第二个代码片段中设置 $content 变量。在第三个示例中,我们使用前面带有 `t(特殊字符)的文本来初始化 $content,以将文本附加到 tab 后面。

类似地,在第四个示例中,我们使用 @() 表示的数组运算符来创建一个字符串数组,其中每个字符串都是我们一次附加到文件中的一行文本。最后,在第五个示例中,我们使用带有特殊字符的@()来以表格形式附加数据;这些特殊字符包括换行符和制表符。您可以在下面找到不同的特殊字符及其描述。

Special Character

描述

`0

无效的

`a

警报

`b

退格键

`n

换行符

`r

回车

`t

水平制表符

`'

单引号

`"

双引号

在本节的第七个也是最后一个示例中,我们在初始化 $content 的值时使用了单引号和双引号。请注意,每个引号(单引号和双引号)前面都有一个反引号,如上表所示。

如果该文件(在本例中为 file.txt)不存在,则 Out-File cmdlet 将创建该文件并执行其余操作。

使用 Add-Content Cmdlet

使用 Add-Content cmdlet 将内容附加到 file.txt 文件中的同一行。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.

使用 Add-Content cmdlet 将内容附加到 file.txt 文件中的新行。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.

使用 Add-Content cmdlet 在 file.txt 文件中附加带有特殊字符(例如制表符)的内容。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

使用 Add-Content cmdlet 在 file.txt 文件中附加多行。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

使用 Add-Content cmdlet 在 file.txt 文件中附加表格内容(格式化内容)。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

使用 Add-Content cmdlet 在 file.txt 文件中附加包含单引号和双引号的行。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the eighth line having `'single quote`' and `"double qoutes`"."
Add-Content -Path $filePath -value $content
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the eighth line having 'single quote' and "double quotes".

我们使用 Add-Content 将内容添加到给定文件。我们可以通过直接在命令中指定文本来使用此 cmdlet,或者将其存储在对象中并在使用 Add-Content cmdlet 时使用该对象。例如,我们将内容存储在 $content 变量中,并在使用此 cmdlet 时使用该变量;请参阅上面的代码围栏。

本节的示例与使用 Out-File cmdlet 的示例类似。再次,我们定义并初始化了 $filePath$content 以包含文件路径和内容。 Get-Content 用于从 file.txt 文件获取内容并将其显示在终端上。存储在 $content 变量中的内容的格式与我们在上一节中使用 Out-File cmdlet 时所做的完全一样;你可以参考一下。

在本节的所有示例中,我们使用 -Path-value 参数以及 Add-Content cmdlet 来指定 $filePath $content

如果文本文件尚不存在,Add-Content 将创建具有指定名称的文本文件。如果未给出路径,则将在当前目录中创建文本文件。

使用重定向 (>>) 运算符

使用 >> 运算符将内容附加到 file.txt 文件中的同一行。

使用重定向运算符:

$filePath = "E:\Test\file.txt"
$content = "This is the second line."
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.

使用 >> 运算符将内容附加到 file.txt 文件中的新行。

使用重定向运算符:

$filePath = "E:\Test\file.txt"
$content = "This is the third line."
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.

使用 >> 运算符在 file.txt 文件中附加带有特殊字符(例如制表符)的内容。

使用重定向运算符:

$filePath = "E:\Test\file.txt"
$content = "`tThis fourth line is with tab character."
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.

使用>> cmdlet 在file.txt 文件中附加多行。

使用重定向运算符:

$filePath = "E:\Test\file.txt"
$content = @("This is the fifth line.",
          "This is the sixth line.",
          "This is the seventh line.")
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.

使用 >> 运算符在 file.txt 文件中追加表格内容(格式化内容)。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = @("First`tLast`tAge",
          "John`tWilliamson`t50",
          "Mary`tPurell`t30",
          "Sam`tAnderson`t35")
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35

使用 >> 运算符在 file.txt 文件中附加包含单引号和双引号的行。

使用添加内容 Cmdlet:

$filePath = "E:\Test\file.txt"
$content = "This is the seventh line having `'single quote`' and `"double quotes`"."
$content>>$filePath
Get-Content $filePath

输出 :

This is the first line.This is the second line.
This is the third line.
        This fourth line is with tab character.
This is the fifth line.
This is the sixth line.
This is the seventh line.
First   Last    Age
John Williamson   50
Mary   Purell 30
Sam    Anderson     35
This is the seventh line having 'single quote' and "double quotes".

这些示例、cmdlet 和变量与前面部分中的相同,但我们使用重定向运算符来附加 file.txt 文件。

如果文本文件尚不存在,则重定向运算符将在指定目录(如果未给出,则在当前目录)中创建文本文件。

将一个文本文件附加到另一个文本文件

使用 Add-Content 将一个文本文件的内容附加到 PowerShell 中的另一个文本文件中。

file1.txt文件的内容:

This is the file1 line.

file2.txt文件的内容:

This is the file2 line.

使用添加内容 Cmdlet:

$sourceFile = "E:\Test\file2.txt"
$destinationFile = "E:\Test\file1.txt"
$sourceFileContent = Get-Content -Path $sourceFile
Add-Content -Path $destinationFile -value $sourceFileContent 
Get-Content $destinationFile

输出:

This is the file1 line.This is the file2 line.

这就是如何使用 PowerShell 将数据附加到文件的全部内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯