Solved

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

Posted on 2011-03-11
6
869 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

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 54

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

739 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