Reputation: 3433
I'm trying to figure out how to determine if the directory "\Program Files (x86)\Apache Software Foundation\Apache2.2" exists on any of the local drives of a Windows 2008 machine.
I thought it'd be something like this:
for /f "skip=2 tokens=2 delims=," %%A in (
'wmic logicaldisk get name /format:csv'
) DO (
echo %%A
IF EXIST "%%A\Program Files (x86)\Apache Software Foundation\Apache2.2" (
echo FOUND ON %%A
)
)
But it simply echos the drive letter.
I'm a Unix guy stuck in a Windows world for this particular project, please help!
I got some help from a co-worker, here's how it was solved:
SET FOUNDIT=FALSE
SET ERRORLEVEL=
FOR /F "skip=2 tokens=2 usebackq delims=," %%i IN (
`wmic logicaldisk where "drivetype=3" get name^,size /format:csv`
) DO (
if /i "%foundit%" equ "true" exit /b
CALL :APACHE %%i
if %ERRORLEVEL% equ 0 set FOUNDIT=TRUE
)
IF %FOUNDIT% equ "TRUE" exit /B ELSE exit /B 1
:APACHE
set DRIVE=%1
REM echo DRIVE is %DRIVE%
if "%DRIVE%" equ "" exit /B
set APACHEPATH=%DRIVE%\Program Files (x86)\Apache Software Foundation\Apache2.2
IF EXIST "%APACHEPATH%" CALL :FOUNDIT "%APACHEPATH%"
exit /B
:FOUNDIT
echo FOUND APACHE on %1
for /f "tokens=*" %%x in (%1) do set APACHEINSTALLDIR=%%x
set FOUNDIT=TRUE
exit /B
Thanks for the help!
Upvotes: 2
Views: 196
Reputation: 56180
wmic has a ugly behaviour: it does not write proper linefeeds, so if you use the last token, you run into problems.
Workaround: don't use the last token (or force it to add another token, that you don't use, here size
). As you don't use the last token, it doesn't matter that it doesn't end properly
for /F "skip=2 tokens=2 delims=," %A in ('wmic logicaldisk where "drivetype=3" get name^,size /format:csv') DO (
(you will have to escape the comma).
I added the where "drivetype=3"
, so it will only check HardDisks (no CD/DVD/Flash), but you can leave this out.
Upvotes: 3
Reputation: 9545
An another way a bit simplist but secure and much more faster as the WMIC
method :
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
if exist %%a: (
IF EXIST "%%a:\Program Files (x86)\Apache Software Foundation\Apache2.2\" (
echo FOUND ON [%%a:])))
Upvotes: 0