Solved

Batch Variable Behavior in Blocks

Posted on 2008-10-22
3
210 Views
Last Modified: 2010-04-21
Hello,

In a batch program I have a variable that performs different when within a block of code (see snippet).  If the code is executed in an IF statement  block %CONNECTIONS_VALUE% equals "" (double quotes).  If I remark out the IF statement start and end lines the value returned is correct.

What needs to be changed in standard Windows batch files to retain variables set in a block of code?

Thanks,

Michael

set CHECK_CONNECTIONS=1
 
if not %CHECK_CONNECTIONS% == 0 (
   set CONNECTIONS_VALUE=25
)
 
echo CONNECTIONS_VALUE: %CONNECTIONS_VALUE%

Open in new window

0
Comment
Question by:michael4606
[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
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 22783936
Works fine for me both ways.... odd.  So you are saying that

set CHECK_CONNECTIONS=1
 
REM if not %CHECK_CONNECTIONS% == 0 (
   set CONNECTIONS_VALUE=25
REM )
 
echo CONNECTIONS_VALUE: %CONNECTIONS_VALUE%

returns "" (actually "" and not blank?).  For me it returns 25 as expected?

Is this actual cut and paste of what you are doing or amended, could be you have additional spaces in or something etc.

Steve
0
 

Author Closing Comment

by:michael4606
ID: 31508877
>> could be you have additional spaces in or something

Perhaps so.  Also it seems I needed to use: setlocal ENABLEDELAYEDEXPANSION
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22785302
You shouldn't need to do that, that is normally used within FOR loops so that a result computed as part of the script is computed each time around rather than once when the script is run but heh if it works for your script great!

thanks
Steve
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

691 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