?
Solved

Batch Variable Behavior in Blocks

Posted on 2008-10-22
3
Medium Priority
?
211 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 500 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

Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

Question has a verified solution.

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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

764 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