Solved

How to set and recall variables in a BAT file

Posted on 2007-04-03
19
508 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
IT, Stop Being Called Into Every Meeting

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
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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 …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

21 Experts available now in Live!

Get 1:1 Help Now