Solved

Batch Variable Behavior in Blocks

Posted on 2008-10-22
3
208 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

830 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