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
effectively:
if (%1 == <a directory>) goto part2
rem process non directory
goto end
:part2
rem directory processing
:end
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, I was just about to ask about the File not found from the for... line.
Thanks alot
SF.
Thanks alot
SF.
ASKER
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.
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
@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
ASKER
Cool - thanks dude - I remembered why I used to hated DOS batch programming :)
SF
SF
It certainly is much improved from those days :)
ASKER
I'm afraid I do all my tricky batch processing in linux scripts these days. Its what your used to I guess.
@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