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.
SURESH0518Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Giovanni HewardCommented:
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

0
SURESH0518Author Commented:
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
0
Giovanni HewardCommented:
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

0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

SURESH0518Author Commented:
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
0
Giovanni HewardCommented:
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

0
SURESH0518Author Commented:
Is it possible to send full script together so that I will execute. Thanks a lot
0
Giovanni HewardCommented:
Here's a sample.  You may want to edit in Notepad++ for syntax highlighting to make things easier.

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

Environment=AAA
Reportid=4
ReportInstance=LLL
InputParameters=TEST
OutputFilename=TEST.DOC
PRINTREPORT=YES

good to go, do stuff..

example: launch.exe AAA 4 LLL
example: launch.exe AAA 4 LLL TEST TEST.DOC YES
etc.


@echo off
setlocal enabledelayedexpansion
set args=%*

call :check Environment
call :check Reportid
call :check ReportInstance
call :check InputParameters
call :check OutputFilename
call :check PRINTREPORT
if [!error!]==[true] goto :eof


rem do stuff
echo.
echo good to go, do stuff..
echo.
echo example: launch.exe !Environment! !Reportid! !ReportInstance!
if not [!InputParameters!]==[] if not [!OutputFilename!]==[] if not [!PRINTREPORT!]==[] echo example: launch.exe !Environment! !Reportid! !ReportInstance! !InputParameters! !OutputFilename! !PRINTREPORT!
if [!Reportid!]==[4] echo Processing report 4 here...
echo etc.


goto :eof

:check
call :arg %1
if [!found!]==[true] (
     set %1=!value!
     echo %1=!%1!
) else (
     for %%z in (Environment Reportid ReportInstance) do (
		if [%1]==[%%z] (
			echo %%z argument is required, aborting.
			set error=true
		)
	)
)
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SURESH0518Author Commented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.