-1

i created a new folder with:

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

and copy an older in the new with:

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

Now are in the new folder ini. files from the old folder and in the ini files are some paths how refer on ohter ini files. I want to change the paths in the ini files of the new folder.

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

I tried;

$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
}

but it changed only the content of the ini files and not the Path what are inside of the ini file. I dont no what is from can someone help me please?

  • 1
    `for each` is incorrect use: `ForEach` or `ForEach-Object`. – Jelphy Jul 11 '17 at 15:17
  • 2
    Possible duplicate of [Powershell INI editing](https://stackoverflow.com/questions/22802043/powershell-ini-editing) – henrycarteruk Jul 11 '17 at 15:19
  • If you use the [PsIni](https://www.powershellgallery.com/packages/PsIni) module you can use `Get-IniContent` to read the file into a hashtable that is very easy to edit/update and `Out-IniFile` to save it back to an ini file. – henrycarteruk Jul 11 '17 at 15:46
  • i want to use the script on more than one Computer and when i use PsIni i must Install it on every pc. So i prefer to do this without PsIni – marten1234 Jul 12 '17 at 07:26

1 Answers1

0

I tried this put it change only the content of the ini files in the example New Folder to the NewPath and do nothing in the supfolder $NewPath = New-Item "C:\exampleNew\exampleNew" -ItemType directory

$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
}