我在Windows XP上使用Powershell,并试图编写一条命令,该命令将:
1. read all .bat,.cfg, and .config files
2. replace a string (it's actually the path...these files were all moved)
3. overwrite the existing files with the new one (same name, same location, etc.)
我不是Powershell用户,但是我设法将以下各项拼凑起来:
gci -r -include "*.bat","*.config","*.cfg"
| gc
| foreach { $_ -replace "D:","C:\path" }
| sc ??.FullName
我相当确定我已经处理过#1和#2,但是在弄清楚#3时遇到了麻烦(将文件名传递给可以在sc中引用的变量)。有什么想法吗?另外,如果您需要其他任何信息,请告诉我。
编辑:
我设法得出一个答案(见下文),但是有更好的方法吗?
最佳答案
尝试:
gci -r -include "*.bat","*.config","*.cfg" |
foreach-object { $a = $_.fullname; ( get-content $a ) |
foreach-object { $_ -replace "D:","C:\path" } |
set-content $a }