?
Solved

Batch scripting :Argument reading

Posted on 2007-07-25
6
Medium Priority
?
3,347 Views
Last Modified: 2012-08-13
PROBLEM 1 :
I want to run a batch script (xyz.bat ) that takes ,say, 3 tags and their corresponding values :
Example of the command  : xyz.bat  -a 12 -b 23 -c 14

How can i  check how many arguments i have passed to this script and iterate over each and every argument .

I would want the script to take parse the command line arguments and perform the task based on the tag
The exact operation to be done :

Parse ( command line arguments )
if ( -a ) :
        set variable1 to 12
if ( -b ) :
        set variable2  to 23
if ( -c ) :
        set variable3  to 14

end

PROBLEM 2 :

how to check if the 2nd argument is a string and not null similar to
if [ -n $2 ] check ,  in shell scripts.
0
Comment
Question by:bhaskar20001
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 19571291
REM - This batch file will set an environment variable for each tag you pass in.
REM - An empty tag will not create a variable, so if you pass in   -a 12 -b -c 14   ... there would be no "b" variable set.



@echo off

:loop

 if "%1"=="" goto done
 set token=%1

 if "%token:~0,1%"=="-" (set tag=%token:~1% & set val=) else (set val=%1)
 set %tag: =%=%val%
 shift

 goto :loop

:done
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 600 total points
ID: 19571311
REM - a slightly improved version


@echo off

:loop

 if "%1"=="" goto done
 set token=%1

 if "%token:~0,1%"=="-" (set tag=%token:~1%) else (set %tag: =%=%1)

 shift

 goto :loop

:done

 cls
 set
0
 
LVL 4

Author Comment

by:bhaskar20001
ID: 19581422
Thanks a lot for the solution.I would like to know the following check also in batch :

if(2nd argument is string and not null )
{
do something
}

Thanks
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Expert Comment

by:knightEknight
ID: 19582050
There isn't really such a thing as a "null" argument to a batch file.  If by this you mean there is no matching value for a tag argument, you can do this if you know ahead of time what the possible tag values will be.


REM example:  C:\> xyz.bat -a 11 -b -c 33 -d 44



@echo off

setlocal

:loop

 if "%1"=="" goto done
 set token=%1

 if "%token:~0,1%"=="-" (set tag=%token:~1%) else (set %tag: =%=%1)

 shift

 goto :loop

:done

if "%a%"=="" (echo parameter a is null) else (echo parameter a is %a%)

if "%b%"=="" (echo parameter b is null) else (echo parameter b is %b%)

if "%c%"=="" (echo parameter c is null) else (echo parameter c is %c%)

if "%d%"=="" (echo parameter d is null) else (echo parameter d is %d%)

:end

endlocal
0
 
LVL 4

Author Comment

by:bhaskar20001
ID: 19591291
Thanks a lot for your help.It solved the problem
0
 
LVL 4

Author Comment

by:bhaskar20001
ID: 19591317
Thanks a lot for your help.It solved the problem
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question