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

2024-04-13

我在 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 中引用的变量)。有什么想法吗?另外,如果您需要任何其他信息,请告诉我。

EDIT:

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


try:

gci -r -include "*.bat","*.config","*.cfg" |
 foreach-object { $a = $_.fullname; ( get-content $a ) |
 foreach-object { $_ -replace "D:","C:\path" }  | 
set-content $a }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐