Powershell:递归替换目录的选择子文件中的字符串

Dav*_*d C 7 powershell file-io windows-xp

我在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.)
Run Code Online (Sandbox Code Playgroud)

我不是Powershell的用户,但是我设法将以下内容拼凑在一起:

gci -r -include "*.bat","*.config","*.cfg" 
    | gc 
    | foreach { $_ -replace "D:","C:\path" } 
    | sc ??.FullName
Run Code Online (Sandbox Code Playgroud)

我很确定我已经处理了#1和#2,但我很难搞清楚#3(将文件名传递给sc中可以引用的变量).有什么想法吗?另外,如果您需要任何其他信息,请与我们联系.

编辑:

我设法找到答案(见下文),但是有更好的方法吗?

CB.*_*CB. 10

尝试:

gci -r -include "*.bat","*.config","*.cfg" |
 foreach-object { $a = $_.fullname; ( get-content $a ) |
 foreach-object { $_ -replace "D:","C:\path" }  | 
set-content $a }
Run Code Online (Sandbox Code Playgroud)