Solved

How to set and recall variables in a BAT file

Posted on 2007-04-03
19
514 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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 …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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