Link to home
Start Free TrialLog in
Avatar of sheepfarmer
sheepfarmer

asked on

Directory detection in CMD batch processing

Using cmd batch processing under XP, is it possible to determine if a passed parameter is a directory

effectively:
if (%1 == <a directory>) goto part2
rem process non directory
goto end

:part2
rem directory processing

:end
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Try this:

@echo off

setlocal

if not exist "%~1" echo %1 does not exist&goto :EOF

for /f %%a in ('dir /b /ad "%~1"') do goto :DIRFOUND

echo This is not a directory
goto :EOF

:DIRFOUND

echo This is a directory

Good Luck,
Steve
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sheepfarmer
sheepfarmer

ASKER

Yes, I was just about to ask about the File not found from the for... line.

Thanks alot

SF.
Just found a little bug.
If the directory specified does not contain any other directories, the dir /b /ad does not return anything and the call to DIRFOUND does not happen (rightly so).

Any ideas?  What does the /f bring to the party?

SF.
Good catch. Here's another version that appears to work a little better:

@echo off

setlocal

if "%~1"=="" echo usage: %0 dirName&goto :EOF
if not exist "%~1" echo %1 does not exist&goto :EOF

set dirName=%~1
if not "%dirName:~-1%"=="\" set dirName=%dirName%\

if exist "%dirName%" goto :DIRFOUND

echo %~1 is not a directory
goto :EOF

:DIRFOUND

echo %~1 is a directory
Cool - thanks dude - I remembered why I used to hated DOS batch programming :)

SF
It certainly is much improved from those days :)
I'm afraid I do all my tricky batch processing in linux scripts these days.  Its what your used to I guess.