Solved

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

Posted on 2011-03-11
6
866 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
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 53

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
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 video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

828 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