Solved

How to set and recall variables in a BAT file

Posted on 2007-04-03
19
509 Views
Last Modified: 2010-04-16
Hello,

I have a batch file that I would like to set a variable based on which file exists on the disk.  This part I have figured out.  I am stuck on an easy part as to how to set a variable based on that file and how to recall that variable later.  I am basically trying to dynamically map a drive based on where a computer is located.  The batch I have is as follows:

@ECHO OFF

REM "Get correct TeraStation IP Address"
if exist "c:\location1.dat" (
  SET TeraStation = 123.246.2.14
) else (
  if exist "c:\location2.dat" (
    SET TeraStation = 63.224.65.2
  ) else (
    SET TeraStation = 123.246.2.14
  )
)

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: \\%TeraStation%\SmartLaunch babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: \\123.246.2.14\World of Warcraft
)

CLS
Exit


As you can see from the batch, I am mapping 1 drive based on a variable name.  I am also checking that variable later and if it equals a certain number, I am mapping a second drive.  For some reason, when it gets to the mapping part, the %TeraStation% shows up as blank, even though I have an "ELSE" statement setting it in case neither file exists.  Please help!

ED7
0
Comment
Question by:electricd7
  • 7
  • 6
  • 5
  • +1
19 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18843009
Change this...assuming that babadook is the password?  If not enclose the whole thing in quotes..

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: "\\123.246.2.14\World of Warcraft"
)



As for your initial logic...try this instead:

SET TeraStation = 123.246.2.14
if exist "c:\location1.dat" (
  SET TeraStation = 123.246.2.14
) else (
  if exist "c:\location2.dat" (
    SET TeraStation = 63.224.65.2
   )
)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18843033
Instead of nesting things (and using spaces) try this:

@ECHO OFF

REM "Get correct TeraStation IP Address"
REM Default Terastation value
SET TeraStation = 123.246.2.14
REM If the location information is not 1 then set the value differently
if exist "c:\location1.dat" SET TeraStation=63.224.65.2

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
if "%TeraStation%" = "123.246.2.14" net use n: "\\%terastation%\World of Warcraft"


CLS
Exit



Also, enclose paths in " - those with spaces don't usually work well unless you've defined them through being enclosed in quotes.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 18843065
leew caught the space problems I was going to mention.  That just leaves the problem with the IF command.  Use == or EQU, not =.
0
 

Author Comment

by:electricd7
ID: 18843070
Ok that will work, but I will soon have 3-4 locations.  How do I modify this to work with more than 2 options?  Thanks.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 18843092
How about a loop?

for /f %%a in ('dir c:\location*.dat') do goto %%a
goto :eof

:location1.dat
Set TeraStation = 123.246.2.14
goto map
:location2.dat
Set TeraStation = 63.224.65.2
goto map
:location3.dat
Set TeraStation = 123.246.2.14
goto map

:map
REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: "\\123.246.2.14\World of Warcraft"
)

Of course, you can continue this model for as many as needed...this assumes only one location#.dat file will exist in the root...
0
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 250 total points
ID: 18843101

@echo off
If exist "c:\location1.dat" Goto Location1
If exist "c:\location2.dat" Goto Location2
If exist "c:\location3.dat" Goto Location3
If exist "c:\location4.dat" Goto Location4
Goto LocationError

:Location1
Set TeraStation=63.224.65.2
Goto DoneWithLocations

:Location2
Set TeraStation=123.246.2.14
Goto DoneWithLocations

:Location3
Set TeraStation=a.b.c.d
Goto DoneWithLocations

:Location4
Set TeraStation=w.x.y.z
Goto DoneWithLocations

:LocationError
echo Your location could not be determined.  Contact your administrator.  You are NOT connected to the TeraStation.
Pause
:DoneWithLocations

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
if "%TeraStation%" == "123.246.2.14" net use n: "\\%terastation%\World of Warcraft"


CLS
Exit
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18843111
Correction to the first line...
for /f %%a in ('dir c:\location*.dat') do goto %%a
should read

for /f %%a in ('dir c:\location*.dat /b') do goto %%a
0
 

Author Comment

by:electricd7
ID: 18843134
Thanks all.  I am trying leew's suggestion now.  If it works correctly i will award him the points.  I assume yours will work too sirbounty, but I simply like the method he is using a little better.... stand by.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18843193
Actually, I like sirbounty's suggestion - hadn't thought of it.  If you like both our suggestions and both work, I would encourage a points split.
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: 18843207
Ok, just ran Leews suggestion and still getting a blank variable for TeraStation here is what I have:

C:\Documents and Settings\Batch>net use * /del /y
There are no entries in the list.


C:\Documents and Settings\Batch>net use s: "\\\SmartLaunch"
System error 53 has occurred.

The network path was not found.


C:\Documents and Settings\Batch>if "" == "63.224.65.2" net use
 n: \\\World of Warcraft
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18843223
Thanx leew! :^)

Under your :DoneWithLocations
place a

:DoneWithLocations
echo TeraStation is now set to %terastation%
pause
0
 

Author Comment

by:electricd7
ID: 18843226
Yea I like both, but leew's gives me the option to change the "location1.dat" file to a more meaningful name.  But both answer exactly what I asked so long as I can get them not to show up blank variable names in the drive mappings.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18843269
What version of Windows are you using?  (I just tested this on XP Pro and had no problem whatsoever.
0
 

Author Comment

by:electricd7
ID: 18843304
XP is what I am using....
0
 

Author Comment

by:electricd7
ID: 18843306
let me try again....
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18843319
Just curious - how are you generating the location#.dat file?
Perhaps you could just have one generic name: location.dat and drop the IP address inside it?
That way you merely read the file to get the associative IP address...
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18843331
Another excellent idea.  I'm unfortunately late for an appointment - I'll be back later but for now, electricd7, you're in good hands with sirbounty.
0
 

Author Comment

by:electricd7
ID: 18843334
You are right....it is working now.  Not sure what I was smoking??  Well I will be using leew's suggestion, but shall I split points if leew thinks SirBounty's is better (it works too, i tried it!)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18843355
Very glad to assist.
Thanx leew for the nod.  

Have a great day gents.
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…
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 I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

19 Experts available now in Live!

Get 1:1 Help Now