Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-11
6
Medium Priority
?
877 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 57

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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…
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…

688 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