Solved

Help with batch functions and variables

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
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…

777 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