How to parse command line in batch?

April 4, 2011 at 00:55:14
Specs: Windows XP
My batch file is xmake.bat, which only accepts the formatted command line:
xmake codename=g26 action=new lang=en:chs

My question is: how to parse command line in batch file?

// xmake.bat content
:: %* stands for 'codename=g26 action=new lang=en:chs'
::
:: keywords = value
::
:: (keywords,value) pair
::
:: for each 'KEY'(codename, action, lang, the word before equal sign(=)) and it's 'VALUE', set 'VARIABLE' to 'VALUE'
:: eg. SET CODENAME = G26 & SET ACTION = NEW & SET LANG = EN:CHS
::
::
FOR %%A IN (%*) DO (
IF %%A:~0,?(here use substring) == codename (or CODENAME) SET CODENAME = g26
IF %%A:~0,?(here use substring) == action (or ACTION) set ACTION = new
IF %%A:~0,?(here use substring) == lang (or LANG) set LANG = en:chs
)

echo %CODENAME%
echo %ACTION%
echo %LANG%

:: should output g26 new en:chs
:: some code pieces
perl make.pl %CODENAME% %ACTION% %LANG%
:: my xmake.bat file content goes end here
:: Thanks for your help


See More: How to parse command line in batch?

Report •

#1
April 24, 2011 at 06:34:49
@ECHO OFF
SETLOCAL

SET /A token_count=1

:Parse_Params
IF "%~1" EQU "" GOTO Parse_Params_End
FOR /F "tokens=%token_count%" %%A IN ("%*") DO (
    FOR /F "tokens=1-2 delims==" %%B IN ("%%A") DO (
        SET %%B=%%C
    )
)
SET /A token_count+=1
SHIFT
GOTO Parse_Params
:Parse_Params_End

:: Verify variables
echo codename=%CODENAME%
echo action=%ACTION%
echo lang=%LANG%

perl make.pl %CODENAME% %ACTION% %LANG%

EXIT /B

When your only tool is a hammer, every problem looks like a nail.


Report •

#2
April 24, 2011 at 09:31:40
Easier:
for %%a in (%*) do set %%a
echo codename=%CODENAME%
echo action=%ACTION%
echo lang=%LANG%

How To Ask Questions The Smart Way


Report •

#3
April 24, 2011 at 20:03:29
Hmm... Doing it the "easier" way, I get this result:
Environment variable action not defined
Environment variable new not defined
Environment variable lang not defined
Environment variable en:chs not defined
Environment variable codename not defined
Environment variable g26 not defined
codename=
action=
lang=

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
April 25, 2011 at 05:59:01
Welp, I'm going senile.

You're right; CMD breaks up arguments on '='. I must be doing too much VBS and PS these days . . .

Mon 2011.04.25  8.24.14
H:\>copy con a.cmd
@for %%a in (%*) do @echo %%a^Z
        1 file(s) copied.

Mon 2011.04.25  8.27.05
H:\>a asdf=1234
asdf
1234

Mon 2011.04.25  8.27.11
H:\>copy con a.vbs
For Each a In WScript.Arguments
  WScript.Echo a
Next^Z
        1 file(s) copied.

Mon 2011.04.25  8.27.44
H:\>cscript //nologo a.vbs asdf=1234
asdf=1234

Mon 2011.04.25  8.27.57
H:\>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS H:\> echo '$args' > a.ps1
PS H:\> .\a asdf=1234
asdf=1234

How To Ask Questions The Smart Way


Report •

#5
April 25, 2011 at 14:53:09
CMD does break up arguments "=" on but %* still contains them. It's actually the for loop that is spiting it up in this case. Standard for splits unquoted strings on space, tab, comma, semi-colon and equals.

It would be possible to do something like this:

set var=%*
for %%a in ("%var: =" "%") do set %%a


Report •

#6
April 25, 2011 at 17:52:45
Fiiii~iiine
:loop
@echo %1
@shift
@if not "%~1"=="" goto loop^Z
        1 file(s) copied.

E:\>a asdf=1234
asdf
1234

E:\>

Happy now?

How To Ask Questions The Smart Way


Report •

#7
April 25, 2011 at 18:19:38
@Razor2.3

Not really, I was saying that %* still contains the "=", not the numbered arguments.

C:\Documents and Settings\Judago>copy con argtest.bat
@echo off
echo All Args=%*
:loop
@echo %1
@shift
@if not "%~1"=="" goto loop

set var=%*
for %%a in ("%var: =" "%") do echo %%a
^Z
        1 file(s) copied.

C:\Documents and Settings\Judago>argtest.bat codename=g26 action=new lang=en:chs
All Args=codename=g26 action=new lang=en:chs
codename
g26
action
new
lang
en:chs
"codename=g26"
"action=new"
"lang=en:chs"

C:\Documents and Settings\Judago>


Report •

Ask Question