需求:
我正在使用Windows命令行环境编写批处理文件脚本,并希望将文件中某些文本(例如"FOO")的每次出现更改为另一个(例如"BAR").最简单的方法是什么?任何内置功能?
示例:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"
解释一下:
- powershell 启动powershell.exe,它包含在Windows 7中
- -Command "… " 是powershell.exe的命令行arg,包含要运行的命令
- (gc myFile.txt)读取内容myFile.txt(gc是Get-Content命令的缩写)
- -replace ‘foo’, 'bar’只需运行replace命令替换foo为bar
- | Out-File myFile.txt 将输出传递给文件 myFile.txt
- Powershell.exe应该已经成为PATH语句的一部分,但如果没有,您可以添加它.它在我的机器上的位置是-encoding ASCII