ini在文本中编辑路径

时间:2017-07-13 21:37:00

标签: powershell replace ini backslash slash

我做了一个litle脚本wath更改ini文件中的路径文件夹和foldr的子文件夹中的内容。所以我的问题是我看到有\ D:\example\的路径和/ F:/example/的路径。我的脚本仅更改\并且我想包含/但我失败了。你能救我吗?

这是我的剧本

<# 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文件。有没有办法一次做而不是两次?当你帮助我时,它会很棒。

1 个答案:

答案 0 :(得分:0)

您可以使用PowerShell&#39; -replace并逃避反斜杠。示例:"\\\" -replace "\\","/"返回///