Solved

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

Posted on 2007-03-30
7
5,474 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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