Efficiently passing multiple switches to a batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Boris Ent
Posts: 1
Joined: 25 Jun 2013 18:43

Efficiently passing multiple switches to a batch file?

#1 Post by Boris Ent » 25 Jun 2013 19:52

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

____
¯¯¯¯

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Efficiently passing multiple switches to a batch file?

#2 Post by Aacini » 25 Jun 2013 20:02

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Efficiently passing multiple switches to a batch file?

#3 Post by Squashman » 25 Jun 2013 20:47

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Efficiently passing multiple switches to a batch file?

#4 Post by Squashman » 25 Jun 2013 21:06

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Efficiently passing multiple switches to a batch file?

#5 Post by penpen » 26 Jun 2013 03:24

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Efficiently passing multiple switches to a batch file?

#6 Post by Squashman » 26 Jun 2013 04:18

Very Good. I like it.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Efficiently passing multiple switches to a batch file?

#7 Post by Aacini » 26 Jun 2013 04:45

@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

Post Reply