[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2007-03-30
7
Medium Priority
?
5,521 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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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