Link to home
Start Free TrialLog in
Avatar of SURESH0518
SURESH0518

asked on

Windows Batch Scripting

Set Environment=%1
Set Reportid=%2
Set ReportInstance=%3
SET InputParameters=%4
SET OutputFilename=%5
SET PRINTREPORT=%6

First Three parameters Mandatory and next three are optional. How do I write windows batch script so that user may or may not pass 4,5 and 6 parameters. Any help is appreciated.
Avatar of Giovanni
Giovanni
Flag of United States of America image

Just create your script to account for omission of these parameters.

For example:
if not [%4]==[] (
     echo Process InputParameters
)
if not [%5]==[] (
     echo Process OutputFilename
)
if not [%6]==[] (
     echo Process PRINTREPORT
)

Open in new window

Avatar of SURESH0518
SURESH0518

ASKER

I did not address this issue properly say I have .bat file and I want to run a.bat with named parameter Environment="AAA" Reportid=4 ReportInstance="LLL" InputParameters=
OutputFilename= PRINTREPORT=Yes. and my output should be
Environment=AAA
Reportid=4
ReportInstance=LLL
PRINTREPORT=Yes

If I passs a.bat Environment="AAA" Reportid=4 ReportInstance="LLL" InputParameters=test
OutputFilename= PRINTREPORT=Yes my output should be
Environment=AAA
Reportid=4
ReportInstance=LLL
InputParameters=test
PRINTREPORT=Yes

Any idea how to write batch script
This will get you closer...

script.bat Environment="AAA" Reportid=4 ReportInstance="LLL" InputParameters=test OutputFilename="" PRINTREPORT=Yes

Output:
[ENVIRONMENT]=[AAA]
[REPORTID]=[4]
[REPORTINSTANCE]=[LLL]
[INPUTPARAMETERS]=[TEST]
[PRINTREPORT]=[YES]


The position of the argument makes no difference.  Currently this is case insensitive and null values either need to be enclosed in quotes or the arg omitted altogether.

@echo off
setlocal enabledelayedexpansion
set args=%*

for %%z in (Environment Reportid ReportInstance InputParameters OutputFilename PRINTREPORT) do (
	call :arg %%z
	if [!found!]==[true] (
		echo [!option!]=[!value!]
	)
)
goto :eof

:arg
set c=1
set arg=%1
call :UCase arg
for %%a in (!args:=^= !) do (
	set str=%%a
	call :UCase str
	if [!arg!]==[!str!] if [!c!]==[1] set option=!str!
	if [!c!]==[2] if [!arg!]==[!option!] (
			set value=!str:"=!
			if not [!value!]==[] set found=true
			goto :eof
	)
	if [!c!]==[2] set c=0
	set /a c=c+1
	set found=false
)
goto :eof

:LCase
for %%i in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") do call set "%1=%%%1:%%~i%%"
goto :eof

:UCase
for %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do call set "%1=%%%1:%%~i%%"
goto :eof

Open in new window

Thanks for you solution. Is there any way can I set a each value to a variable so that these values can be in other program. I mean is it possible to say SET ENVIRONMENT=AAA
SET REPORTID=4
SET REPORTINSTANCE=LLL
SET INPUTPARAMETERS=TEST
SET PRINTREPORT=YES
Yes, just create a new var as needed :

call :arg Environment
if [!found!]==[true] (
     set Environment=!value!
     echo Environment=!Environment!
) else (
     echo Environment argument is required, aborting.
     goto :eof
)

Open in new window

Is it possible to send full script together so that I will execute. Thanks a lot
ASKER CERTIFIED SOLUTION
Avatar of Giovanni
Giovanni
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Everything works perfectly as you said.One question When pass ENVIRONMENT=Dev32 script is not working since it looks for call :check Environment and program assumes it is case sensitive. Is there any way could we ignore case sensivity?