Solved

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

Posted on 2007-03-30
7
5,464 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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