Solved

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

Posted on 2007-03-30
7
5,455 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:electricd7
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
Thanks, I'm glad it worked :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now