Iswarya
Iswarya

Reputation: 323

Conditional statements in batch files

Is it possible to have conditional statements in batch scripts?

For example:

I have two servers, S1 and S2.When the batch file is deployed in S1 then the output should be generated in location L1. Likewise if the batch file is deployed in S2 then output should be generated in location L2.

My script:

set ComputerName=S1
set RepServer=%ComputerName%
set DBServer=%ComputerName%
set ReportPath="/DEV/Clearviewbilling"
set SharedPath=\\scottvdr1\ClearviewBilling\DEV-TEST
set UserId=-E
set fn=Create_Log.txt

if exist %fn% del %fn%
@echo on

@rem Reports
rs -i "%CD%"\Reports\Create_Sub.rss -s http://%RepServer%/reportserver -v Path=%SharedPath% -v rootpath=%ReportPath% -v DBServer=%DBServer% -t  >>  %fn% 2>&1

But i want the script to be:

set ComputerName=S1
set RepServer=%ComputerName%
set DBServer=%ComputerName%

If ComputerName=S1
Set SharedPath=//blah/blah
else
Set sharedPath=//some/path

set ReportPath="/DEV/Clearviewbilling"
set UserId=-E
set fn=Create_Log.txt

if exist %fn% del %fn%
@echo on

@rem Reports
rs -i "%CD%"\Reports\Create_Sub.rss -s http://%RepServer%/reportserver -v Path=%SharedPath% -v rootpath=%ReportPath% -v DBServer=%DBServer% -t  >>  %fn% 2>&1   

Hence when the file is deployed, the reports are generated in the required path. But this is not working.

Upvotes: 10

Views: 23191

Answers (1)

jeb
jeb

Reputation: 82337

You compare the string ComputerName against S1 with the wrong if-else syntax

This should work

if "%ComputerName%"=="S1" (
    Set "SharedPath=//blah/blah"
) else (
    Set "sharedPath=//some/path"
)

Upvotes: 18

Related Questions