Solved

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

Posted on 2011-03-11
6
863 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
Comment Utility
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
Comment Utility
"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
Comment Utility
Additional Info:
Current OS is XP.
Needs to work on 2003, 2008 as well.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
BAM! Thanks!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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…

728 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

11 Experts available now in Live!

Get 1:1 Help Now