Solved

Help with batch functions and variables

Posted on 2011-03-11
5
372 Views
Last Modified: 2012-05-11
My variables aren't behaving as expected. Can anybody tell me what I'm doing wrong and how to do it correctly?

The first snippet always fails regardless if the file exists or not.
The second snippet works as expected.

@echo off
if 1 == 1 (
type c:\21.bat >nul
call:errorcheck %errorlevel%
)
goto end

:errorcheck
set err=%~1
echo %err%
if "%err%" == "0" (
echo [ok]
) else (
echo [failed]
)
goto:eof

:end

Open in new window

@echo off
type c:\21.bat >nul
call:errorcheck %errorlevel%
goto end

:errorcheck
set err=%~1
echo %err%
if "%err%" == "0" (
echo [ok]
) else (
echo [failed]
)
goto:eof

:end

Open in new window

0
Comment
Question by:jasonzx
[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
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 35112301
That's "early" variable variable expansion.
The batch interpreter reads a statement in brackets as a single line and expands the variables in there when it reads this line.
In order to expand variables only when accessed, you need to enable delayed expansion and use ! to expand variables where necessary:
@echo off
setlocal enabledelayedexpansion
if 1 == 1 (
type c:\21.bat >nul
call:errorcheck !errorlevel!
)
goto end

Open in new window

0
 

Author Comment

by:jasonzx
ID: 35112846
Thanks oBdA, I'll try it at work on Monday(no Windows machines at home).
Do I have to replace  %err% with !err! inside of 'errorcheck' as well?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 35113169
No, the normal expansion works just fine there.
The issue is described in the "set" help as well. Just enter "help set" in a command prompt.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 35115731
BTW, usually you should not use this way to test for existence of a file. Using "type" will still require the file to be read completely. It's better to use if exist:
@echo off
setlocal enabledelayedexpansion
if 1 == 1 (
  set err=0
  if not exist c:\21.bat set err=1
  call :errorcheck !err!
)
exit /b 0

Open in new window

I've also showed a way to end the batch file without using goto.
0
 

Author Comment

by:jasonzx
ID: 35139999
Thanks oBdA, that worked like a charm.

Qlemo:Thanks for the heads up, but that wasn't the real code I was having trouble with, just a stripped down and modified version to demonstrate the problem I was having.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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.
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
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…

756 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