问题描述
我正在尝试替换与条件不匹配的文件中的某些行,然后我需要将输出写入一个 diff 文件 [我实际上需要将输出重定向到同一个文件,但我不知道如何这样做]并得到以下错误
I am trying to replace certain lines in a file that doesnt match the condition and then i need to write the output to a diff file [ I actually need the output to be redirected to the same file, but i dont know how to do it] and get the below error
代码 -
$fileName = E:\onlinecode\afarm_2106.txt
$FileModified = @()
[System.IO.StreamReader]$afarmfile = [System.IO.File]::Open("E:\onlinecode\Btnbar64\a_farm.txt",
[System.IO.FileMode]::Open)
while (-not $afarmfile.EndOfStream){
$line = $afarmfile.ReadLine()
if ( $Line -like "*2104 Training Markets*" )
{
$FileModified += $Line
# Write-Output $FileModified
}
else
{
$FileModified += $Line -replace "2104","2106" -replace "21043","21062"
}
}
$afarmfile.Close()
Set-Content -Path $filename -value $FileModified
错误 -
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:19 char:19
+ Set-Content -Path $filename -value $FileModified
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand
对解决问题的任何帮助/直接写入同一文件将不胜感激
Any help to resolve the issue / Directly write to the same file would be appreciated
推荐答案
考虑使用 Theo 改进的解决方案,但只是为了指出当前的问题是什么:
Consider using Theo's improved solution, but just to point out what the immediate problem was:
您的 $fileName =
赋值的 RHS 缺少引用:
The RHS of your $fileName =
assignment lacked quoting:
# WRONG: Lack of quoting of the RHS string value.
# This *opens the file in a text editor* and assigns $null to $fileName
$fileName = E:\onlinecode\afarm_2106.txt
与 cmd.exe
或 bash
等 shell 不同,为变量分配 string 值需要引用em> 在 PowerShell 中.
Unlike in shells such as cmd.exe
or bash
, assigning string values to variables requires quoting in PowerShell.
由于您的路径是一个文字值,最好使用单引用:
Since your path is a literal value, it's best to use single-quoting:
# OK - string must be *quoted*.
$fileName = 'E:\onlinecode\afarm_2106.txt'
请注意,需要引用字符串适用于所有形式的赋值,包括属性,可能作为哈希表或自定义的一部分-object 文字(例如,@{ foo = 'bar' }
或 [pscustomobject] @{ foo = 'bar' }
.
Note that the need for quoting strings applies to all forms of assignments, including to properties, possibly as part of a hashtable or custom-object literal (e.g., @{ foo = 'bar' }
or [pscustomobject] @{ foo = 'bar' }
.
背景信息:
PowerShell 有两种基本的解析模式、参数模式(类似shell)和表达式模式(类似编程语言).
PowerShell has two fundamental parsing modes, argument mode (like a shell) and expression mode (like a programming language).
第一个字符(在本例中位于 =
的右侧)决定进入哪种模式,并且由于您的值既不以 开头'
, "
, $
, @
, (
, [
>,也没有 {
,argument 模式被输入,并且你的 - 未加引号的 - 文件路径被解释为命令.
It is the first character (on the RHS of =
, in this case) that determines which mode is entered, and since your value started neither with '
, "
, $
, @
, (
, [
, nor {
, argument mode was entered, and your - unquoted - file path was interpreted as a command.
PowerShell 接受一个文件路径来执行,即使文件本身不是可执行的,在这种情况下,它会要求 GUI shell 打开文件,就像您在文件中双击它一样例如,Windows 上的资源管理器.
PowerShell accepts a file path for execution even if the file isn't itself executable, in which case it asks the GUI shell to open the file, as if you had double-clicked it in File Explorer on Windows, for instance.
因此,系统的默认文本编辑器中将打开一个未加引号的 .txt
文件路径,并且此操作将 (a) 异步且 (b) 不产生任何输出.
Therefore, an unquoted .txt
file path will open in your system's default text editor, and this operation will (a) be asynchronous and (b) produce no output.
因此,$fileName
没有被赋值,这实际上使它成为 $null
.
As a result, $fileName
was assigned no value, which effectively made it $null
.
这篇关于Powershell - 设置内容:无法将参数绑定到参数“路径",因为它为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!