1

I'm new at batches. I'm trying to edit a line in a file using batch. However, the line contains spaces and a reserved character (the = sign) Basically, I want to change PasswordComplexity = 1 to PasswordComplexity = 0, which is in a file named export.cfg All ideas appreciated, here is what I have now:

setlocal enabledelayedexpansion

FOR /F "usebackq delims=" %%a IN ("export.cfg") DO (
set "line=%%a" & echo !line:(PasswordComplexity = 1)=(PasswordComplexity = 0)!
)>>"import.cfg"
Nazik
  • 8,696
  • 27
  • 77
  • 123
Dito
  • 45
  • 1
  • 4

1 Answers1

1

Unfortunately, equal signs = cannot be simply replaced with just native Windows solutions. See https://stackoverflow.com/a/9561827/891976

If the line containing PasswordComplexity only has the single digit, you can do something like this:

setlocal EnableExtensions EnableDelayedExpansion

for /f "usebackq delims=" %%A in ("export.cfg") do (
    set "line=%%A"
    set "found=false"
    for /f "delims=" %%B in (echo %%A^|find "PasswordComplexity = 1") do set "found=true"
    if !found!==true (
        echo !line:1=0!
    ) else (
        echo !line!
    )
)>>"import.cfg"

endlocal

Also note that whenever using special characters literally outside of quotation marks, they must be escaped. See http://www.robvanderwoude.com/escapechars.php

Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Nice try, but wrong! An opending bracket need no escape, the closing does, but the equal can't be escaped in a repalce expression. An equal sign can't be replaced this way – jeb Mar 01 '13 at 20:28