0

What is the most efficient method of passing multiple command line arguments to a batch file in this format?

PACKER.BAT /Action="Pack" /Source="..\path to source folder\" /Target="..\path to target folder\" /Pass="SecretCode" /Output="Packed.exe" /GPGhome="..\path to GnuPG home directory\" /User="UserID" /Admin="AdminID" /Profile="ProfileID"

In the .BAT I want to strip the leading “/” and “SET” the switches as environment variables.

The variables will be passed to 7Zip, GPG, DWipe and some basic error checking.  For use in Vista, Win7 and Win8.

PACKER.BAT notes and snippets...

:Start
:: Prepare the Command Processor & initialise variables
Set colours, title, comments, errorlevels, etc.
Read variables from general profile file, etc.
Process command line arguments

:Lock
if exist %Lock% @echo ž   WARNING:  %ProfileID%  is running.

:Check
if exist %Action%="Pack" do ... else UnPack, etc.
if exist %Source% do ... else error
if exist %Target% do ... else create

:Zip
%7Zexe% u -ms=off -mtc=on -ssw -w%Temp% -p%Pass% -mhe=on %sfx:"=% %Target%%Output% %Source%

:GPG
%GPGexe% --homedir %GPGhome% -r %UserID% -r %AdminID% -e %Target%%Output%

:Wipe
%DWexe% wipe1 %Target%%Output%

:Help
@echo Allowable switches for %ProfileID%:
@echo %Menu:"=%

:End
:: clean up environment variables

¯¯¯¯

Boris Ent
  • 3
  • 2
  • Take a look at this question: [Windows Bat file optional argument parsing](http://stackoverflow.com/q/3973824/1012053), especially [my answer](http://stackoverflow.com/a/8162578/1012053). It shows how to formally define your options, possibly with default values, and provide error handling. – dbenham Jun 26 '13 at 11:20

1 Answers1

3
@echo off

rem Erase all existent variables to show the created ones at end
setlocal EnableDelayedExpansion
for /F "delims==" %%a in ('set') do set %%a=

rem Get the parameters and define variables with them
set var=
for %%a in (%*) do (
   if not defined var (
      set var=%%a
   ) else (
      set !var:~1!=%%~a
      set var=
   )
)

rem Show all defined variables
set

For example, if you execute:

PACKER.BAT /Action="Pack" /Source="..\path to source folder\" /Target="..\path to target folder\" /Pass="SecretCode" /Output="Packed.exe" /GPGhome="..\path to Gn uPG home directory\" /User="UserID" /Admin="AdminID" /Profile="ProfileID"

the output is:

Action=Pack
Admin=AdminID
GPGhome=..\path to GnuPG home directory\
Output=Packed.exe
Pass=SecretCode
Profile=ProfileID
Source=..\path to source folder\
Target=..\path to target folder\
User=UserID
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This works fine as long as none of the arguments contain `*`, `?`, or `!`. The `?` problem can be solved easily enough with search and replace. The `!` problem can be solved with considerable effort. But the `*` problem is nearly impossible (definitely impractical) to solve. – dbenham Jun 26 '13 at 10:56
  • Thanx Aacini, for such an elegant answer. How do I keep the values quoted and pass them onto the various processes within the batch file? Cheers ____ ¯¯¯¯ – Boris Ent Jun 27 '13 at 06:25
  • To preserve the quotes just remove the second tilde `~` in this line: `set !var:~1!=%%~a`. You already have examples in your code on how to pass the values of the variables... – Aacini Jun 27 '13 at 12:49