0

I have the following loop:

FOR %%a IN (%*) DO (

    IF "%%a"=="-D" (
        # Do something
    )
)

I want to check if "%%a" starts with "-D", not equals.

I know about %var:~0,2% staff, but I don't see how to use it here.

Update

@Mofi's answer does solve the problem, but:

@echo off

SET JVM_ARGS=-server 

setlocal EnableExtensions DisableDelayedExpansion

FOR %%a IN (%*) DO (
     set "Argument=%%~a"
     setlocal EnableDelayedExpansion


    IF /I "!Argument:~0,2!"=="-D" (
        SET JVM_ARGS=%JVM_ARGS% %%a
    )
    endlocal
)

echo %JVM_ARGS%

endlocal

The problem I have is that SET JVM_ARGS=%JVM_ARGS% %%a doesn't work now, although it is executed. echo %JVM_ARGS% prints -server instead of -server -Dxxx

  • Copy `%%a` to a normal environment variable and do the sub-string expansion with that; regard that you will need [delayed variable expansion](https://ss64.com/nt/delayedexpansion.html) then! – aschipfl Aug 11 '20 at 07:06

3 Answers3

0

One solution is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
SET "JVM_ARGS=-server"
for %%I in (%*) do (
    set "Argument=%%~I"
    setlocal EnableDelayedExpansion
    if /I "!Argument:~0,2!" == "-D" (
        rem echo Argument starting with -D is: !Argument!
        endlocal
        set "JVM_ARGS=%JVM_ARGS% %%I"
    ) else endlocal
)
rem set JVM_ARGS
endlocal

It works for:

  • -d
  • -D
  • "-d"
  • "-D"
  • -dhello
  • -DHello
  • -d"Hello world!"
  • -D"!That's my function()!"
  • "-dHello world!"
  • "-DYes, argument processing is not trivial!"

The above code is written for just one -D option. For multiple -D options it would be necessary to replace the line

        set "JVM_ARGS=%JVM_ARGS% %%I"

by

        set "Argument=%%I"
        call set "JVM_ARGS=%%JVM_ARGS%% %%Argument%%"

The command CALL results in parsing the command line being already

        call set "JVM_ARGS=%JVM_ARGS% %Argument%"

after parsing the entire command block before executing FOR a second time with replacing %JVM_ARGS% by current string value of this environment variable and %Argument% by the string value assigned to environment variable Argument above. The additional Argument environment variable is needed to avoid that a string after -D containing % is malformed as it would occur with using %%I instead of %%Argument%%.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • setlocal /?

Please read this answer for details about the commands SETLOCAL and ENDLOCAL.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

This code solves the issues:

@echo off

SET JVM_ARGS=-server 

setlocal  EnableDelayedExpansion

FOR %%a IN (%*) DO (
echo %%a
    set "_arg_=%%~a"
    IF "!_arg_:~0,2!"=="-D" (
        SET "JVM_ARGS=!JVM_ARGS! %%a"
    )

)

echo %JVM_ARGS%

endlocal
0

just to give you an alternative without delayed expansion:

FOR %%a IN (%*) DO (
    echo %%a|findstr /bc:"-D" >nul && (
        echo Do something
    )
)

which simply checks, if %%a starts with -D (instead of checking if the first two chars are -D), which gives the same results.

You may want to add findstr's i switch to make it case-insensitive (findstr /bic:"-D")

Stephan
  • 53,940
  • 10
  • 58
  • 91