2017-07-13 33 views
0

編輯在我的INI文件是什麼文件夾中,並在foldr相似的子文件夾做了一個痘痘腳本wath變化路徑。所以我的問題是,我看到有與\D:\example\和路徑與/F:/example/路徑。我的腳本只更改了\,並且包含/,但我失敗了。你可以幫我嗎?INI路徑文本

這裏我的腳本

<# 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 

這並不工作,以及何時工作,它需要更多的時間,因爲我必須在文件夾和子文件夾讀取所有INI文件。有沒有辦法做到一次而不是兩次?當你無法幫助我時,它會很棒。

回答

0

您可以使用PowerShell的-replace並轉義反斜槓。例如:"\\\" -replace "\\","/"返回///