2017-07-13 8 views
0

J'ai fait un petit script wath modifier les chemins dans les fichiers ini qui sont dans un dossier et dans le sous-dossier du foldr. Donc mon problème est que je vois qu'il y a des chemins avec \D:\example\ et le chemin avec /F:/example/. Mon script ne change que \ et je dois inclure / mais j'échoue. Pouvez-vous m'aider?ini Chemin d'édition dans le texte

Voici mon script

<# here i create a new folder with the folder dialog #> 
Ad-Type-AssemblyName 
syste,-windows.Forms 
$browser = New-Object system.windows.Forms.FolderBrowserDialog 
$browser.Description " Make A New Folder 
$null = $browser.showDialog() 
$browser = $NewPath 

<# here i select a folder what i want to copy with the folder dialog #> 
Ad-Type-AssemblyName 
syste,-windows.Forms 
$browser = New-Object system.windows.Forms.FolderBrowserDialog 
$browser.Description " Make A New Folder 
$null = $browser.showDialog() 
$browser = $OldPath 

<# Here i copy the old in the new folder #> 
Copy-Item $OldPath\* $NewPath -recurse 

<# Here i replace the old Paths with backslashes in the ini files (they are in the text of the ini file) with the new #> 

$ChangePath = $OldPath 
$iniFile = Get-ChildItem -Path "$NewPath/* include *.ini -recurse 
ForEach($file in $iniFile){ 
    (Get-Content $file)-replace [regex]::Escape($ChangePath),($NewPath) | Set-Content - Path $file.FullName 

<# this uper the comment work but now come what doesnt work #> 
<# here i try to replace all paths in the text of the ini file with a normal slash "/" #> 

$changePath2 = $OldPath 
($changePath2 -replace [regex]::Escape ('\'),('/')) 
$NewPath = $NewPath 
($NewPath2 -replace [regex]::Escape ('\'),('/')) 
$iniFile2 = Get-ChildItem -Path "$NewPath/* include *.ini -recurse 
ForEach($file2 in $iniFile2){ 
    (Get-Content $file2)-replace [regex]::Escape($ChangePath2),($NewPath2) | Set-Content - Path $file2.FullName 

Ce travail ne marche pas et quand il travaillerait-il besoin de plus de temps parce que je dois lire tous les fichiers ini dans le dossier et le sous-dossier. Y a-t-il un moyen de le faire une fois et pas dans deux? Ce serait génial quand vous ne pouvez pas m'aider.

Répondre

0

Vous pouvez simplement utiliser le -replace de PowerShell et échapper à la barre oblique inverse. Exemple: "\\\" -replace "\\","/" renvoie ///