如何将标题添加到文本文件中?

时间:2022-06-01 12:42:02

How can I add the header to a top of text file using PowerShell? I have tried using the below code like:

如何使用PowerShell将标题添加到文本文件的顶部?我已经尝试使用以下代码:

Add-Content -Path E:\BHS_output_1.txt -Value (Get-Content "E:\header.txt")

1 个解决方案

#1


1  

Like this:

是这样的:

$textfile   = 'E:\BHS_output_1.txt'
$headerfile = 'E:\header.txt'
$(Get-Content $headerfile; Get-Content $textfile) | Set-Content $textfile

Read the contents of header and text file after each other in a subexpression, then write the entire output back to the text file. The subexpression is required, so that reading from the text file is completed before writing starts, otherwise you'd get an access conflict.

在子表达式中逐个读取头文件和文本文件的内容,然后将整个输出写回文本文件。必须使用子表达式,以便在开始写入之前完成文本文件的读取,否则会出现访问冲突。

#1


1  

Like this:

是这样的:

$textfile   = 'E:\BHS_output_1.txt'
$headerfile = 'E:\header.txt'
$(Get-Content $headerfile; Get-Content $textfile) | Set-Content $textfile

Read the contents of header and text file after each other in a subexpression, then write the entire output back to the text file. The subexpression is required, so that reading from the text file is completed before writing starts, otherwise you'd get an access conflict.

在子表达式中逐个读取头文件和文本文件的内容,然后将整个输出写回文本文件。必须使用子表达式,以便在开始写入之前完成文本文件的读取,否则会出现访问冲突。