3

How to get not splited by the equal sign attributes inside batch script?

For example:

script.bat option1 -DpropertyName_1="propertyValue_1" -DpropertyName_2="propertyValue_2" -DpropertyName_n="propertyValue_n"

My script.bat

if ""%1""==""option1"" goto setJavaOpts
if ""%1""==""option2"" goto doneSetJavaOpts

:setJavaOpts
shift
if ""%1""=="""" goto doneSetJavaOpts
set JAVA_OPTS=%JAVA_OPTS% %1
goto setJavaOpts
:doneSetJavaOpts
VladAdjima
  • 31
  • 2
  • 1
    It's not an easy task without seeing the batch file. Have you tried with something like `"-DpropertyName_1=propertyValue_1"`? – MC ND Nov 08 '18 at 13:01
  • 1
    `=` is a standard delimiter for batch files (like spaces and tabs) , but probably you need this to call a java program which will not accept jvm arguments enclosed in double quotes. So you'll need to pass the whole command line to the java like `java.exe %*` and to add additional arguments if needed. – npocmaka Nov 08 '18 at 13:06
  • @MC ND Your solution is works, but it's there too much quotes for users, because it's look like `"-DpropertyName_1="propertyValue_1""` – VladAdjima Nov 08 '18 at 13:42
  • @npocmaka Actually, I wasn't honest with you. I have another one parameter before -DpropertyName (I've updated my example). It seems, i cannot pass the whole line. – VladAdjima Nov 08 '18 at 13:45
  • if those `-DpropertyName_X` are always at the end then you can use `shift` to remove the starting options and pass the whole properties at once – phuclv Nov 08 '18 at 13:52
  • I'm not sure about your environment, but I think the inner quotes are not needed. – MC ND Nov 08 '18 at 14:19
  • @phuclv What do you mean? If use `shift` then `%*` the first option won't be remove. – VladAdjima Nov 13 '18 at 06:17
  • yeah indeed it doesn't work. Probably you'll need to [get everything after %1](https://stackoverflow.com/q/935609/995714) and pass to another batche file. Or you can remove the `%1` part with [variable substring](https://ss64.com/nt/syntax-substring.html) – phuclv Nov 13 '18 at 06:39

0 Answers0