【问题标题】:ini Path editing in in textini 文本中的路径编辑
【发布时间】:2023-04-01 00:22:01
【问题描述】:

我做了一个小脚本,改变了 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 文件。有没有办法一次而不是两次?如果你能帮助我,那就太好了。

【问题讨论】:

    标签: powershell replace ini backslash slash


    【解决方案1】:

    您可以只使用 PowerShell 的 -replace 并转义反斜杠。示例:"\\\" -replace "\\","/" 返回///

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2014-03-04
      • 2014-07-19
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      相关资源
      最近更新 更多