简体   繁体   English

使用Powershell替换目录中所有文件中的值

[英]Use powershell to replace value in all files in directory

This is probably an easy one, but I'm trying to write a script to use when moving a directory for an installed program from one computer to another. 这可能很简单,但是我正在尝试编写一个脚本,以便将已安装程序的目录从一台计算机移动到另一台计算机时使用。 In order to make this work, I have to find every instance of the old hostname, old IP address, and old drive letter from the old machine, and replace them with the new hostname, new IP address, and the new drive letter on the new machine. 为了使此工作正常进行,我必须从旧计算机上找到旧主机名,旧IP地址和旧驱动器号的每个实例,并用新主机名,新IP地址和新驱动器号替换它们。新机器。 The primary folder contains *.xml and *.config files that need to be edited in the primary folder as well as in sub folders. 主文件夹包含* .xml和* .config文件,这些文件需要在主文件夹以及子文件夹中进行编辑。

This is the code I'm working with: 这是我正在使用的代码:

$oldIP = "192.168.1.2"
$newIP = "192.168.1.3"
$oldhost = "OLDHOSTNAME"
$newhost = "NEWHOSTNAME"
$oldDriveLetter = "C"
$newDriveletter = "E"

$path = "\\$newip\$newdriveletter$\Folder"
$files = get-childitem $path\* -include *.xml, *.config, -recurse 
$files | %{
    (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Folder", "${newDriveLetter}:\Folder" | set-content $_.fullname
}

Currently, it is only replacing the values in the primary folder, but not any of the sub folders. 当前,它仅替换主文件夹中的值,而不替换任何子文件夹中的值。 Any idea what I'm missing? 知道我缺少什么吗?

Edit: Per the suggestion below, I removed the comma after *.config, and that seems to get me through the sub folders. 编辑:根据下面的建议,我删除了* .config之后的逗号,这似乎使我通过了子文件夹。 But it's still not replacing any instance of C:\\Folder with E:\\Folder 但是它仍然没有用E:\\ Folder替换任何C:\\ Folder实例

This works fine. 这很好。 Took the comma off the end of *.config, and added another \\ in the middle of ${olddriveletter}:\\Folder. 将逗号放在* .config末尾,并在$ {olddriveletter}:\\ Folder中间添加另一个\\。

$oldIP = "192.168.1.2"
$newIP = "192.168.1.3"
$oldhost = "OLDHOSTNAME"
$newhost = "NEWHOSTNAME"
$oldDriveLetter = "C"
$newDriveletter = "E"

$path = "."
$files = get-childitem $path\* -include *.xml, *.config -recurse 
$files | %{
    (gc $_) -replace $oldhost, $newhost -replace $oldip, 
       $newip -replace "${olddriveletter}:\\Folder","${newDriveLetter}:\Folder" | 
       set-content $_.fullname
}

Tried to streamline it a little. 试图简化一下。 Too bad you can't just do "get-childitem | get-content -replace | set-content". 太糟糕了,您不能只执行“ get-childitem | get-content -replace | set-content”。

get-childitem $path\* -include *.xml, *.config -recurse | 
foreach {
  (get-content $_) -replace $oldhost,$newhost -replace $oldip,
  $newip -replace "${olddriveletter}:\\Folder", "${newDriveLetter}:\Folder" |
  set-content $_ 
}

I suggest to: 我建议:


## Q:\Test\2019\05\09\SO_56064191.ps1

$oldIP = "192.168.1.2"
$newIP = "192.168.1.3"
$oldhost = "OLDHOSTNAME"
$newhost = "NEWHOSTNAME"
$oldDriveLetter = "C"
$newDriveletter = "E"

$path = '\\{0}\{1}$\Folder' -f $newip,$newdriveletter

ForEach($File in (Get-ChildItem $path\* -Include *.xml,*.config -Recurse)){
    (Get-Content $File.FullName -raw) -replace [RegEx]::Escape($oldhost),$newhost `
                                 -replace [RegEx]::Escape($oldip),$newip `
                                 -replace "$olddriveletter(?=:\Folder)",$newDriveLetter | 
     Set-Content $File.FullName -NoNewline
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM