Powershell:递归替换目录的选择子文件中的字符串 [英] Powershell: Recursively Replace String in Select Sub-files of a Directory

查看:480
本文介绍了Powershell:递归替换目录的选择子文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows XP上使用Powershell,并试图编写一个命令:

I'm using Powershell on Windows XP and am trying to write a command that will:

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用户,但是我设法零碎以下一起:

I am not a Powershell user, but I have managed to piecemeal the following together:

gci -r -include "*.bat","*.config","*.cfg" 
    | gc 
    | foreach { $_ -replace "D:","C:\path" } 
    | sc ??.FullName

我相当肯定我已经照顾了#1和# 2,但是在计算#3时遇到了困难(将文件名传递给可以在sc中引用的变量)。有什么想法吗?此外,让我知道如果你需要任何额外的信息。

I'm fairly certain that I've taken care of #1 and #2, but am having trouble figuring out #3 (passing the filename to variable that can be referenced in sc). Any thoughts? Also, let me know if you need any additional information.

编辑:

我设法得出答案(见下文),但有没有更好的方法来做到这一点?

I managed to work out an answer (see below), but is there a better way to do this?

推荐答案

尝试:

try:

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

这篇关于Powershell:递归替换目录的选择子文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆