Search code examples
batch-filecmdenvironment-variablesescapingspecial-characters

identify or delete equal sign (=) in Windows command script environment variable


Is there methods to do the subject? For example, we can't simply replace equal sign usual way through substring replacing syntax %variable:substring1=substring2%, because substring1 can't contain equal sign.


Solution

  • What about a simple loop that walks through the string and checks every character against =?

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "STRING=%~1" & rem // (string from first command line argument)
    set "SEARCH==" & rem // (specify a single character here)
    set "REPLAC=" & rem // (specify an arbitrary string here)
    
    rem // Check search string for validity (one character):
    if not defined SEARCH ((>&2 echo ERROR: no search string defined!) & exit /B 1)
    setlocal EnableDelayedExpansion
    if not "!SEARCH:~1!"=="" ((>&2 echo ERROR: search string too long^^!) & exit /B 1)
    rem // Loop through each character of the string:
    set "RESULT="
    :LOOP
    if not defined STRING goto :QUIT
    rem // Compare current character with search string:
    set "CHAR=!STRING:~,1!"
    if "!CHAR!"=="!SEARCH!" (
        rem // Match found, so replace character:
        set "RESULT=!RESULT!!REPLAC!"
    ) else (
        rem // No match found, so keep character:
        set "RESULT=!RESULT!!CHAR!"
    )
    rem // Remove processed character from (remaining) string:
    set "STRING=!STRING:~1!"
    goto :LOOP
    :QUIT
    rem // Return result here finally:
    echo(!RESULT!
    endlocal
    
    endlocal
    exit /B
    

    This should be a bit better in terms of performance, because there are less string manipulations:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "STRING=%~1" & rem // (string from first command line argument)
    set "SEARCH==" & rem // (specify a single character here)
    set "REPLAC=" & rem // (specify an arbitrary string here)
    
    rem // Check search string for validity (one character):
    if not defined SEARCH ((>&2 echo ERROR: no search string defined!) & exit /B 1)
    setlocal EnableDelayedExpansion
    if not "!SEARCH:~1!"=="" ((>&2 echo ERROR: search string too long^^!) & exit /B 1)
    rem // Loop through each character of the string:
    set /A "INDEX=0" & set "RESULT="
    if not defined STRING goto :QUIT
    :LOOP
    rem // Compare currently indexed character with search string:
    set "CHAR=!STRING:~%INDEX%,1!"
    if not defined CHAR goto :QUIT
    if "!CHAR!"=="!SEARCH!" (
        rem // Match found, so replace character:
        set "RESULT=!RESULT!!REPLAC!"
    ) else (
        rem // No match found, so keep character:
        set "RESULT=!RESULT!!CHAR!"
    )
    rem // Increment character index:
    set /A "INDEX+=1"
    goto :LOOP
    :QUIT
    rem // Return result here finally:
    echo(!RESULT!
    endlocal
    
    endlocal
    exit /B