删除 List.txt 文件中空行的命令,当我使用 variable 时不起作用$env:username
。
作品:在文件路径中使用 CMG 用户名。
$linblk = [System.IO.File]::ReadAllText('C:\Users\CMG\Desktop\List.txt');$linblk = $linblk.Trim();[System.IO.File]::WriteAllText('C:\Users\CMG\Desktop\List.txt', $linblk)
不起作用:$env:username
在文件路径中使用变量:
$linblk = [System.IO.File]::ReadAllText('C:\Users\$env:username\Desktop\List.txt');$linblk = $linblk.Trim();[System.IO.File]::WriteAllText('C:\Users\$env:username\Desktop\List.txt', $linblk)
Exception when calling "ReadAllText" with "1" argument(s): "The given path format is not supported."
No line:1 character:1
+ $linblk = [System.IO.File]::ReadAllText('C:\Users\$env:username\Deskt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如何使命令接受变量$env:username
?
已编辑:
所以它起作用了:答案中的命令@Vomit IT - Chunky Mess Style
$linblk = [System.IO.File]::ReadAllText("C:\Users\$($env:username)\Desktop\List.txt");
$linblk = $linblk.Trim();
[System.IO.File]::WriteAllText("C:\Users\$($env:username)\Desktop\List.txt", $linblk)
用于
[System.IO.File]::ReadAllText("C:\Users\$env:username\Desktop\List.txt")
将参数值封装在双引号中的语法。电源外壳
PowerShell(另一种变体)
支持资源