我在批处理文件中运行 Powershell 命令来替换文本。
这可行,但每次替换某些东西时它都会运行一个 Powershell 实例:
:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | Set-Content $_.FullName}"
:: Replace foo2 with bar2
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | Set-Content $_.FullName}"
:: Replace foo3 with bar3
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"
& ^
有没有办法通过在第二条命令的末尾添加类似的东西来链接三个命令?
我想将每个-replace
命令放在单独的行上,因为我有 100 多个命令要链接在一起。
我试图这样做,但它不起作用:
:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | & ^
:: Replace foo2 with bar2
| ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | & ^
:: Replace foo3 with bar3
| ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"
我也试过上面没有&
字符的方法。
我尝试ForEach-Object {(Get-Content $_)
从中间和最后一行删除,不起作用。
它是:: comments
造成问题的原因吗?
我想既然这是用双引号括起来的 Powershell 命令,我会尝试这种方式,每行末尾的反勾号和分号应该告诉 Powershell 继续执行命令并将下一行视为相同的命令:
:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`
# Replace foo2 with bar2
| ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2';`
# Replace foo3 with bar3
| ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"
(不起作用)
我尝试取出一些 Powershell 命令:
:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`
# Replace foo2 with bar2
'foo2', 'bar2';`
# Replace foo3 with bar3
'foo3', 'bar3' | Set-Content $_.FullName}"
(不起作用)。
我已经尝试完全删除评论,不起作用。
我已经尝试添加回-replace
每行的开头,但不起作用。
试图把它放在一条线上是行不通的:
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1'; -replace 'foo2', 'bar2'; -replace 'foo3', 'bar3' | Set-Content $_.FullName}"
放置管道而不是分号也不起作用:
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | -replace 'foo2', 'bar2' | -replace 'foo3', 'bar3' | Set-Content $_.FullName}"
可能有几十种不同的组合试图让它发挥作用,但我不知道正确的方法是什么,而且我没有想法。
;
当与其他答案中描述的类似命令链接在一起时,这些命令不起作用。
干杯。
您想将许多命令连续放置并从批处理文件中调用它吗?
您的方法不仅不起作用,因为您不能在批处理文件中的一个命令中放入这么多字符,而且也不可能那么容易地将它们链接起来。
这里明显的解决方案是要么只使用 PowerShell,要么将所有命令放在 .ps1 文件中,并将批处理文件中的 powershell 命令更改为:
要编辑 .ps1 文件,请使用 PowerShell ISE。这不仅为您提供了语法高亮显示,您还可以通过运行一个命令而不是整个脚本来非常轻松地测试您正在执行的操作,并且您在脚本中设置的每个变量在执行后都会保留,因此您可以在脚本运行后在控制台中进行实时测试运行以调试工作不正常的地方。
您将在批处理文件中运行的每个命令也可以在 PowerShell 中运行。即使是逻辑等,PowerShell 也提供了功能,因此您也可以编写更易于阅读的代码。
您可以尝试一条线与倍数
.replace()
:.replace('foo1','bar1').replace('foo2','bar2').replace('foo3','bar3')...