我有以下应该从cmd脚本运行的脚本:
powershell -command "(Get-Content %baseKitPathFile%) | ForEach-Object { $_ -replace 'Latest', '%version%' } | Set-Content %baseKitPathFile%"
该脚本工作正常,并将Latest
的内容替换为version
变量,但是它还在文件末尾添加了回车符
如何在没有额外回车符的情况下搜索替换文本文件内容
可能正在尝试使用[io.file]:
最重要的是if应该从cmd脚本运行
发布于 2015-06-10 12:16:50
Set-Content
和Out-File
都在每行之后换行,包括最后一行。要避免这种情况,必须使用IO.File
方法:
powershell -Command "$txt = (Get-Content %baseKitPathFile%) -replace 'Latest', '%version%'; [IO.File]::WriteAllText('%baseKitPathFile%', $txt)"
不过,PowerShell脚本比上面的命令行更易于处理:
[CmdletBinding()]
Param(
[Parameter()][string]$Filename,
[Parameter()][string]$Version
)
$txt = (Get-Content $Filename) -replace 'Latest', $Version
[IO.File]::WriteAllText($Filename, $txt)
这样叫它:
powershell -File "C:\path\to\your.ps1" "%baseKitPathFile%" "%version%"
https://stackoverflow.com/questions/30751320
复制