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

标签 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.)

我不是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 }

关于Powershell:以递归方式替换目录中选择子文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11368786/

相关文章:

windows - 如何在Windows上配置IBM Kubernetes CLI

javascript - 显示文件输入标签中选择的所有图像

powershell - 如何清除由外部 cmdlet 或可执行文件设置的 $error 和 $LASTEXITCODE?

powershell - 使用 powershell 编辑设备管理器

使用 C 文件处理更改本地路径上的文件名?

重启/强制重启后继续安装 WiX Burn

c++ - 代码注入(inject)在 Vista 或 Win7 上不起作用?

windows - 在 Windows XP 上监视显示器电源事件

windows - 将 IPython 配置为使用 powershell 而不是 cmd

linux - 如何在shell脚本生成的文件顶部写标题