How to detect if folder exist (and ignore file with same name ;))

Today I experienced really annoying bug with one of my scripts. It is checking if one folder (in quite complex structure) exists(If Exist Folder...), if it does, it perfoms some actions and send few mails. However after some time it started to behave strange - it was sending sometimes mail when folder was definitely not there etc.

 It didnt take me long time to discover what was the problem - another team was using file without extension with same name as my folder :) So I rewrote today this script to detect ONLY folder and ignore files with same name.

 How to do it? Its quite simle

 If Exist Folder\nul.ext Send-Mail

For example If Exit C:\Temp\test\nul.ext will be True if folder test exists, however not if file test exists.

You just need to be very careful - Windows doesnt allow you to have folder and file with same name in same folder:

C:\Temp>mkdir Test

C:\Temp>Echo.>test
Access is denied.

C:\Temp>rmdir Test

C:\Temp>Echo.>test

C:\Temp>mkdir Test
A subdirectory or file Test already exists.

C:\Temp>

Published Tue, Jan 30 2007 8:50 by martin
Filed under:

Comments

# re: How to detect if folder exist (and ignore file with same name ;))

I was taught this way to check for a folder.

Code:

if exist folder\. command

Every folder has a . directory, even if empty, so this test works in all versions.

msdn.microsoft.com/.../aa365247(VS.85).aspx

Saturday, May 24, 2008 7:07 AM by Lewis Meyer

# re: How to detect if folder exist (and ignore file with same name ;))

Hi Lewis,

thanks, this post is older and method I described have few problems (mostly related to 8.3 translation), so now I am using same method as you everywhere

Martin

Sunday, May 25, 2008 12:28 PM by martin

# re: How to detect if folder exist (and ignore file with same name ;))

I am really glad to hear it! Internet searches revealed to me that it is fairly common knowledge among linux users, but the .(DOT Directory usage) appears to be not well known among Windows users, even though I was taught this method way back in 1995.

I have also run across a few other places that the .(DOT Directory) comes into play when you need to specify a directory name in a batch file but do not have a name for it in advance.

IE: The root directory name is \. Commands such as RD and SUBST must have a directory name and \. does it for them.

Monday, May 26, 2008 6:13 AM by Lewis Meyer