?
Solved

Need help with an IF Then ElseIf Else statement in windows batch file.

Posted on 2007-03-30
7
Medium Priority
?
5,506 Views
Last Modified: 2010-10-05
I have a batch file that maps various network drives.  The bat file is the same on all computers, however, I would like the drive that gets mapped to be different if a certain file exists.  For example, I want to write into my .bat file to check and see if "c:\file1.dat" exists.  If so, map drive x to 123.123.123.1\c$, elseif "c:\file2.dat" exists map to 123.123.123.1\d$, else map to 123.123.123.1\e$.  I can do this fine with VBScript but just not in Windows Batch.  So basically a simple if/then elseif, else statement.  Thanks in advance!
0
Comment
Question by:electricd7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18825668
Try this:

@echo off

if exist "c:\file1.dat" call :PROCESS c&goto :EOF
if exist "c:\file2.dat" call :PROCESS d&goto :EOF
if exist "c:\file3.dat" call :PROCESS e&goto :EOF

goto :EOF

:PROCESS

net use x: /del 2>NUL
net use x: 123.123.123.1\%~1$

Good Luck,
Steve
0
 

Author Comment

by:electricd7
ID: 18825700
Ok that makes perfect sense for the question I asked, but I should have asked it differently= :)  Chances are that the IP address won't be the same.  So how do I do the same thing and just send the batch to separate parts, so that I can process 1 or 2 lines depending on which file exists.  Make sense?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18825778
Here I have the PROCESS routine accept the drive to be mapped and the URL to be mapped.

@echo off

if exist "c:\file1.dat" call :PROCESS x 123.123.123.1\c$&goto :EOF
if exist "c:\file2.dat" call :PROCESS y 123.123.123.2\d&goto :EOF
if exist "c:\file3.dat" call :PROCESS z 123.123.123.3\e&goto :EOF

goto :EOF

:PROCESS

net use %~1: /del 2>NUL
net use %~1 %~2
0
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

Author Comment

by:electricd7
ID: 18825850
Im still not getting it.  Here is essentially what I want writen in VB

if exists "c:\files1.dat" then
  net use 123.123.123.1\c$
  do something else here
elseif exists "c:\file2.dat" then
  net use 123.123.123.23\c$
  do something else here
else
  net use 123.123.123.34\c$
  do something else here
end if

more bat file lines here
more bat file lines here

Am I just not getting it, or what?
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 2000 total points
ID: 18825885
@echo off

if exist "c:\file1.dat" (
  call :PROCESS x 123.123.123.1\c$
  REM ** Do other stuff
) else (
  if exist "c:\file2.dat" (
    call :PROCESS y 123.123.123.2\d$
    REM ** Do other stuff
  ) else (
    call :PROCESS z 123.123.123.3\e$
    REM ** Do other stuff
  )
)

REM ** Do more stuff
REM ** Do more stuff

goto :EOF

:PROCESS

net use %~1: /del 2>NUL
net use %~1 %~2
0
 

Author Comment

by:electricd7
ID: 18825998
Perfect!  Im sure the other suggestion would have worked the same, but I didn't see what was going on.  I get it.  Here's your points!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18826038
Thanks, I'm glad it worked :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question