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
Windows BatchMicrosoft DOS

Avatar of undefined
Last Comment
sheepfarmer
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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.
Avatar of sheepfarmer
sheepfarmer

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.
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

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
Avatar of sheepfarmer
sheepfarmer

ASKER

Cool - thanks dude - I remembered why I used to hated DOS batch programming :)

SF
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

It certainly is much improved from those days :)
Avatar of sheepfarmer
sheepfarmer

ASKER

I'm afraid I do all my tricky batch processing in linux scripts these days.  Its what your used to I guess.
Microsoft DOS
Microsoft DOS

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.

14K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo