Solved

How to set and recall variables in a BAT file

Posted on 2007-04-03
19
517 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
[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
  • 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 96

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 96

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 96

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
 

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 96

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 96

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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