How to test if a file is a directory in a batch script?

This works:

if exist %1\* echo Directory

Works with directory names that contains spaces:

C:\>if exist "c:\Program Files\*" echo Directory
Directory

Note that the quotes are necessary if the directory contains spaces:

C:\>if exist c:\Program Files\* echo Directory

Can also be expressed as:

C:\>SET D="C:\Program Files"
C:\>if exist %D%\* echo Directory
Directory

This is safe to try at home, kids!

Leave a Comment