Page 1 of 1

Efficiently passing multiple switches to a batch file?

Posted: 25 Jun 2013 19:52
by Boris Ent
What is the most efficient method of passing multiple command line arguments to a batch file in this format?

Code: Select all

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...

Code: Select all

: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

____
¯¯¯¯

Re: Efficiently passing multiple switches to a batch file?

Posted: 25 Jun 2013 20:02
by Aacini
You should wait for an answer in the other forum before cross posting at anoter site: http://stackoverflow.com/questions/1730 ... batch-file

Anyway, this is the answer I posted at that site:

Code: Select all

@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=

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:

Code: Select all

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:

Code: Select all

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


Antonio

Re: Efficiently passing multiple switches to a batch file?

Posted: 25 Jun 2013 20:47
by Squashman
Good Job Antonio.

I was trying the following but the only way I could make it work is if I removed the quotes from the input and ran it like this.

Code: Select all

@echo off

rem Erase all existent variables to show the created ones at end
for /F "delims==" %%a in ('set') do set %%a=
set "parms=%*"
set parms="%parms:~1%"
set parms=%parms: /=" "%
 
FOR %%G IN (%parms%) DO set %%~G
set parms=
set
pause

Code: Select all

C:\batch\packer>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
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

Re: Efficiently passing multiple switches to a batch file?

Posted: 25 Jun 2013 21:06
by Squashman
I should really go to bed when it is late.

Code: Select all

@echo off

rem Erase all existent variables to show the created ones at end
for /F "delims==" %%a in ('set') do set %%a=
set "parms=%*"
set parms=%parms:"=%
set parms="%parms:~1%"
set parms=%parms: /=" "%
 
FOR %%G IN (%parms%) DO set %%~G
set parms=
set
pause


Code: Select all

C:\batch\packer>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"
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
Press any key to continue . . .

Re: Efficiently passing multiple switches to a batch file?

Posted: 26 Jun 2013 03:24
by penpen
Using the set command on all parameter and doing string operations on it is a real good idea Squashman.
Up to now i did it in similar way as Aacini has done it.

But you may max your idea out:

Code: Select all

@echo off
setlocal

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

set "parms=%*"
set parms=set "parms= %parms:"=%"
%parms: /="&set "%

set

pause

endlocal

penpen

Re: Efficiently passing multiple switches to a batch file?

Posted: 26 Jun 2013 04:18
by Squashman
Very Good. I like it.

Re: Efficiently passing multiple switches to a batch file?

Posted: 26 Jun 2013 04:45
by Aacini
@penpen:

Interesting way to avoid the loop. I lke it too! :D

This line makes me to remember jeb's solutions:

Code: Select all

%parms: /="&set "%

Antonio