1.

Solve : Bacth file help?

Answer»

Hey guys, I've been working on a backup batch file to backup data on some machines and now I would like to add some command line switches for my batch file.

Couple of questions:

1) Is there anything you can do about the order of how they add the switches?

C:> EXAMPLE.bat -nodocs -nofavs

c:> example.bat -nofavs -nodocs
--------------------------------------------------------------------------------
example.bat --------------------------------------------------------------------------------

if %1== nodocs set docs=0
if %2== nofavs set favs=0
.
.
.
--------------------------------------------------------------------------------

This is just a crude example. but you see my point. Obviously this would
work for the first example, but would FAIL on the second because the switches
are in the wrong order. Is there a way you can GET AROUND this? I would hate
to tell the other guys that you HAVE to enter the switches in this exact
order.


2) How do you avoid if the user doesn't supply any switches?

c:> example.bat

-------------------------------------------------------------------------------

if %1== nodocs set docs=0
if %2== nofavs set favs=0
.
.
--------------------------------------------------------------------------------



I tried using "if %1== goto noswitch" and "if %1=="" goto noswitch" but both came back with the unexpected return error. This is going to give me an error and terminate because %1 is not defined.

I would also like to add some other switches like a -help so they can see what switches are avaliable. Thanks guys.

Many scripting languages allow you to use both Named and Unnamed arguments. Batch language is not one of them as all command line parameters to a batch file are positional.

You best bet would be to check %1 for all possible switches, then do the same for %2, and so on.

Small error:

example.bat -nodocs -nofavs

if %1== nodocs set docs=0
if %2== nofavs set favs=0

You need to include the - indicator when checking the value of the parameters:

if %1== -nodocs set docs=0
if %2== -nofavs set favs=0

 8-)



Discussion

No Comment Found