Solved

Help with batch functions and variables

Posted on 2011-03-11
5
368 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 83

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 83

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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 teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now