Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there any way to find out if a file is a directory?

I have the file name in a variable. In Perl I can do this:

if(-d $var) { print "it's a directory\n" } 
like image 584
Vhaerun Avatar asked Sep 26 '08 11:09

Vhaerun


People also ask

What is DIR in batch file?

Display a list of files and subfolders.

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.


1 Answers

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!

like image 139
Gerard Avatar answered Sep 29 '22 23:09

Gerard