【问题标题】:Change Path in .ini files with Powershell使用 Powershell 更改 .ini 文件中的路径
【发布时间】:2017-12-15 17:49:42
【问题描述】:

我创建了一个新文件夹:

$NewPath = New-Item "\\...\newExampleFolder -ItemType directory

并在新的中复制一个旧的:

$ OldPath = "D:\...\old.ExampleFolder"
Copy-Item $OldPath* $NewPath -recurse

现在在新文件夹 ini 中。旧文件夹中的文件和 ini 文件中的一些路径是如何引用 ohter ini 文件的。我想更改新文件夹的ini文件中的路径。

Example ini:
[setting1] = 1
[enginge] = 2
[setting2] = \...\old.ExampleFolder\Settings\

我试过了;

$inifiles = Get-ChildItem -Path \\...\newExampleFolder\* include *.ini -
recurse
foreach($file in $inifiles)
{
   (Get-Content $file.PSPath)
   Foreach-Object {$file -replace 
                   [regex]::Escape('D:\...\old.ExampleFolder\'), 
                                  ('\\...\newExampleFolder\') |
                   set-content $file.PSPath
}

但它只更改了 ini 文件的内容,而不是 ini 文件内部的路径。 我不知道是什么,有人可以帮我吗?

【问题讨论】:

  • for each 使用不正确:ForEachForEach-Object
  • Powershell INI editing的可能重复
  • 如果您使用PsIni 模块,您可以使用Get-IniContent 将文件读入一个非常易于编辑/更新的哈希表,并使用Out-IniFile 将其保存回ini 文件。
  • 我想在多台计算机上使用脚本,当我使用 PsIni 时,我必须在每台计算机上安装它。所以我更喜欢在没有 PsIni 的情况下这样做

标签: powershell replace path ini


【解决方案1】:

我试过了,它只将示例 New Folder 中的 ini 文件的内容更改为 NewPath,并且在 supfolder 中什么也不做 $NewPath = New-Item "C:\exampleNew\exampleNew" -ItemType 目录

$OldPath = "C:\exampleOld\exampleOld\"


Copy-Item $OldPath* $NewPath -recurse


$iniFile = Get-ChildItem -Path $NewPath\* -recurse -Include *.ini 
ForEach($inis in $iniFile)
{
(Get-Content $inis.PSPath)
ForEach-Object {$inis -replace [regex]::Escape($OldPath),( $NewPath)}|
Set-Content $NewPath\* -Include *.ini
}

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2015-02-17
    • 2020-12-15
    • 1970-01-01
    • 2022-01-15
    • 2021-10-22
    • 1970-01-01
    • 2011-03-13
    • 2015-03-13
    相关资源
    最近更新 更多