【问题标题】:How do I avoid double commenting a line in an .ini file?如何避免在 .ini 文件中对一行进行双重注释?
【发布时间】:2018-05-23 21:32:48
【问题描述】:

我正在尝试获取和修改 .ini 文件的内容,但是我需要确保我要查找的文本前面没有分号,这将指定该行“已注释掉” ”。

.ini文件包含一个参数块,没有用到的块被注释掉,如下。

;   use the following 2 settings for Sierra external GPS  
;GPSType=Sierra  
;Interface=PPP  
;   use the following 3 settings for internal NMEA GPS  
GPSType=NMEA  
Interface=Serial  
Com=4

我已经在我的管道中尝试了一个 If 语句,但是这是不允许的。

   $EOF = {
     Write-Host "Ending Program"
     break
   }
Write-Host "test Application Internal/External Modem Selector"     -BackgroundColor Green -ForegroundColor Black
Write-Host
Write-Host "*** Please ensure test is not running ***" -ForegroundColor     Yellow
Write-Host
if (!(Test-Path "C:\Program Files (x86)\Rogers\test\test.ini"))
  {
Write-Host "Warning - test.INI not present" -BackgroundColor Black -ForegroundColor Red |
Return(0)
  }
Write-Host
Write-Host "1.  Convert for Internal modem (COM4)"
write-Host "4.  Convert for External modem"
Write-Host "Q.  Exit"
Write-Host
$menuresponse = Read-Host 'Choose the option >'
if ($menuresponse -eq "q") {
&$EOF
  }
elseif ($menuresponse -eq "1") 
  {
(Get-Content "C:\Program Files (x86)\Rogers\test\test.ini") |
  If (($_).notcontains(';Com'))
  {
   {
    ForEach-Object {$_ -replace 'GPSType=Sierra' , ';GPSType=Sierra' } |
    ForEach-Object {$_ -replace 'Interface=PPP' , ';Interface=PPP' } |
    ForEach-Object {$_ -replace ';GPSType=NMEA' , 'GPSType=NMEA' } |
    ForEach-Object {$_ -replace ';Interface=Serial' , 'Interface=Serial' } |
    ForEach-Object {$_ -replace ';Com=.*' , 'Com=4' } |
    Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
   }
  else
   { 
    ForEach-Object {$_ -replace 'Com=.*' , 'Com=4'
    Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
   }
  }
   }}
elseif ($menuresponse -eq "4") 
  {
   (Get-Content "C:\Program Files (x86)\Rogers\test\test.ini") |
 If (($_).notcontains(';Com'))
 {
  { 
   ForEach-Object {$_ -replace ';GPSType=Sierra' , 'GPSType=Sierra' } |
   ForEach-Object {$_ -replace ';Interface=PPP' , 'Interface=PPP' } |
   ForEach-Object {$_ -replace 'GPSType=NMEA' , ';GPSType=NMEA' } |
   ForEach-Object {$_ -replace 'Interface=Serial' , ';Interface=Serial' } |
   ForEach-Object {$_ -replace 'Com=.*' , ';Com=6' } |
   Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
  }
  else
   { 
    Write-Host "Modem already set to External"
    &$EOF
   }
  }
  }
else
{ write-host "Invalid Choice"
}
  Break

有没有办法通过在指令传递之前识别它前面是否有分号来确保我不会在 .ini 文件中“双重注释”一个参数?

【问题讨论】:

  • 尝试使用带有^ 锚点的正则表达式作为replace 的第一个参数。

标签: powershell


【解决方案1】:

我需要确保我要查找的文本前面没有分号

既然您已经在使用正则表达式,为什么不在匹配项中使用字符串锚 ^ 的开头

ForEach-Object {$_ -replace '^GPSType=Sierra' , ';$0' } |

这将取代 GPSType=Sierra,但前提是它位于行首。如果该行已经看起来像 ;GPSType=Sierra 它将不匹配

注意在替换中使用$0。它代表整个匹配字符串。使用它可以帮助防止重复输入。


  • 你真的应该看看PowerShell's choice system。超级容易实现和更面向对象的方法。 Here is an answer of mine 实现它

  • 您可以链接-replace,这样您就不需要多个foreach-object'123' -replace '1','One' -replace '2','Two' -replace '3','Three'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2012-04-24
    • 2020-08-28
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多