Solved

Help with batch functions and variables

Posted on 2011-03-11
5
371 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
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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 will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

840 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