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" }
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" }
Under Windows 7 and XP, I can't get it to tell files vs. dirs on mapped drives. The following script:
produces:
So beware if your script might be fed a mapped or UNC path. The pushd solution below seems to be the most foolproof.
Here's a script that uses FOR to build a fully qualified path, and then pushd to test whether the path is a directory. Notice how it works for paths with spaces, as well as network paths.
Sample output with the above saved as "isdir.bat":
Can't we just test with this :
It seems to work for me.
Based on this article titled "How can a batch file test existence of a directory" it's "not entirely reliable".
BUT I just tested this:
and it seems to work
CD
returns anEXIT_FAILURE
when the specified directory does not exist. And you got conditional processing symbols, so you could do like the below for this.You can do it like so:
However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:
The
%%~si
converts%%i
to an 8.3 filename. To see all the other tricks you can perform withFOR
variables enterHELP FOR
at a command prompt.(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the
%%
with%
in both places.)