Solved

How do I expand an environment variable in a batch file.

Posted on 2011-03-11
6
873 Views
Last Modified: 2012-06-27
I developed the following code as a batch file in an attempt to periodically verify that the security log exists on a system in the location specified in the registry.

The problem is that the path returned from the registry includes %systemroot%\... and this varaiable is not subsequently expanded.   Thus, the IF EXIST check never returns true.

I believe, in theory "setlocal enabledelayedexpansion" should aleviate this problem, but it does not.

There has been a bug reported here, http://www.robvanderwoude.com/variableexpansionbug.php, that may be related.

Given that this may be a bug, I am open to alternative solutions, not including expanding the data as retrieved here.

@echo off
setlocal enabledelayedexpansion
For /F "tokens=3" %%i IN ('REG query HKLM\System\CurrentControlSet\Services\Eventlog\security /v file ^| Find /I ^"File^"') DO (
SET strPath=%%i
echo strPath=%strpath%
IF Exist %strPath% GOTO FOUNDSECLOG
ECHO ALERT! Security Log DOES NOT EXIST at %strPath%.
GOTO END
)
:FOUNDSECLOG
      ECHO Log Found at %strPath%.
:END

Thanks,
JRS
0
Comment
Question by:jrslim
[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
6 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35112221
Does it help to use quotes?  Any space in the expanded strPath will be a problem without them:

IF Exist "%strPath%" GOTO FOUNDSECLOG

Open in new window

0
 

Author Comment

by:jrslim
ID: 35112283
"Does it help to use quotes?  Any space in the expanded strPath will be a problem without them:"
 
No. Single quotes, double quotes, escaped quotes, none helped.  There is no space in the returrned path "%SystemRoot%\System32\config\SecEvent.Evt" before, after, or middle.
0
 

Author Comment

by:jrslim
ID: 35112296
Additional Info:
Current OS is XP.
Needs to work on 2003, 2008 as well.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 35112394
That requires a little trick with a subroutine:
@echo off
setlocal
set strPath=
for /f "tokens=3" %%a in ('reg query "HKLM\System\CurrentControlSet\Services\Eventlog\security" /v "File" ^| find /i "File"') do (
  set strPath=%%a
)
echo strPath before expansion: %strPath%
call :ExpandVar strPath "%strPath%"
echo strPath after expansion: %strPath%
if exist "%strPath%" (
  echo Log Found at %strPath%.
) else (
  echo ALERT! Security Log DOES NOT EXIST at %strPath%.
)
REM More custom code here ...

REM *** Only subroutines from here on ***
goto :eof
:ExpandVar
set %1=%~2
goto :eof

Open in new window

0
 

Author Closing Comment

by:jrslim
ID: 35112440
BAM! Thanks!
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 35113747
Just as an alternative, I think this should also work:

@echo off
setlocal enabledelayedexpansion
For /F "tokens=3" %%i IN ('REG query HKLM\System\CurrentControlSet\Services\Eventlog\security /v file ^| Find /I ^"File^"') DO (
  SET strPath=%%i
  call set strPath=!strpath!
  IF Exist !strPath! (
    ECHO Log Found at %strPath%.
  ) else (
    ECHO ALERT! Security Log DOES NOT EXIST at !strPath!.
  )
)

Open in new window

~bp
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

615 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