Escaping an equals sign in DOS batch string replacement command

后端 未结 4 2248
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 11:20

I need to replace some text in a JNLP file using a DOS batch file to tune it for the local machine.

The problem is that the search pattern contains an equals sign wh

4条回答
  •  情深已故
    2021-01-15 11:39

    The best solution is to download and install Cygwin or GNUWin32 but, if you're really limited to the standard command processor, it can get a little messy.

    This is not the fastest method in the world but it's at least functional. This command file processes each line one character at a time, treating specially the case where you find the stanza you're looking for.

    @echo off
    set init=50M
    set max=75M
    setlocal enableextensions enabledelayedexpansion
    for /f "tokens=* delims=" %%a in (agility.jnlp) do (
        set str1=%%a
        call :morph
        echo !str2!>>agility_new.jnlp
        echo !str2!
    )
    endlocal
    goto :eof
    
    :morph
        set str2=
    :morph1
        if not "x!str1!"=="x" (
            if "!str1:~0,18!"=="initial-heap-size=" (
                set str2=!str2!initial-heap-size="!init!"
                set str1=!str1:~24!
                goto :morph1
            )
            if "!str1:~0,14!"=="max-heap-size=" (
                set str2=!str2!max-heap-size="!max!"
                set str1=!str1:~20!
                goto :morph1
            )
            set str2=!str2!!str1:~0,1!
            set str1=!str1:~1!
            goto :morph1
        )
        goto :eof
    

    With the input file:

    
    next line
    ===
    

    you end up with:

    
    next line
    ===
    

提交回复
热议问题