Solved

Batch Variable Behavior in Blocks

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 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