Sign in to follow this  
Followers 0
Dirk98

If folder exists...

12 posts in this topic

Hello guys,

Could you please paste here a piece of code that checks if a XYZ folder exitst in the path and returns some value if it is not there (does not exists in the path).

Many thanks!

Dirk.

Share this post


Link to post
Share on other sites



Hello guys,

Could you please paste here a piece of code that checks if a XYZ folder exitst in the path and returns some value if it is not there (does not exists in the path).

Many thanks!

Dirk.

FileExists() - Checks if a file or directory exists.

Share this post


Link to post
Share on other sites

FileExists() - Checks if a file or directory exists.

Thank you, Zedna! I used DirMove in my script, but have found it destructive even with 0 flag.

Dirk.

Share this post


Link to post
Share on other sites

While FileExists() will work most of the time, it can return a false positive if a FILE with the name of the DIRECTORY you're looking for exists. For example, let's say you're looking for the DIRECTORY:

C:\WINDOWS\system32\drivers\etc\hosts

Try DirGetSize()

Share this post


Link to post
Share on other sites

While FileExists() will work most of the time, it can return a false positive if a FILE with the name of the DIRECTORY you're looking for exists. For example, let's say you're looking for the DIRECTORY:

C:\WINDOWS\system32\drivers\etc\hosts

Try DirGetSize()

JerryD, thank you very much for the warning. Could you please elaborate the false positive. I don't understand the example you gave there. But sounds serious for my script.

Thanks,

Dirk.

Share this post


Link to post
Share on other sites

His example references the "hosts" file. He is warning that if there is a file with the same name as the directory that you are looking for you will get a false positive.

Lets say you make a folder called C:\Testing

and in it you put a file called "xyz"

now if you tried

$folder="C:\Testing\xyz"
IF FileExist($folder) Then MsgBox(0,"Folder Found","The folder " & $folder & " exists")

The IF statement would return as true even though its not really a folder

1 person likes this

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

His example references the "hosts" file. He is warning that if there is a file with the same name as the directory that you are looking for you will get a false positive.

Lets say you make a folder called C:\Testing

and in it you put a file called "xyz"

now if you tried

$folder="C:\Testing\xyz"
IF FileExist($folder) Then MsgBox(0,"Folder Found","The folder " & $folder & " exists")

The IF statement would return as true even though its not really a folder

Thanks, SpookMeister, got it.

Share this post


Link to post
Share on other sites

I don't mean to change the subject or continue the topic when it has been resolved but looking into it I tried a few things out.

I am wondering if this is sloppy coding, or is it an exceptible way to go about it.

The below code would work, but it does not include the

#include <Array.au3>

or

_ArrayDisplay($FileList,"$FileList")

#include <File.au3>
$FileList = _FileListToArray("C:\" , "*.ini")
If @error Then
    MsgBox(0, "", "File and/or Folder does not exist.")
    Exit
EndIfoÝ÷ Ù8b²Þw%¹×¬¶¦najwp¢¹,"¶+,ºh±çm¡«­¢+Ø¥¹±Õ±Ðí¥±¹ÔÌÐì(%¹±Õ±ÐíÉÉä¹ÔÌÐì(ÀÌØí¥±1¥ÍÐô}¥±1¥ÍÑQ½ÉÉä ÅÕ½ÐíèÀäÈí]¥¹½ÝÌÅÕ½Ðì°ÅÕ½Ð쨹¥¹¤ÅÕ½Ðì¤)%ÉɽÈQ¡¸(5Í   ½à À°ÅÕ½ÐìÅÕ½Ðì°ÅÕ½Ðí¥±¹½½È½±È½Ì¹½Ðá¥ÍиÅÕ½Ðì¤(á¥Ð)¹%)}ÉÉå¥ÍÁ±ä ÀÌØí¥±1¥ÍаÅÕ½ÐìÀÌØí¥±1¥ÍÐÅÕ½Ðì¤

Would any of you more experienced AutoIt ers use the first example or is it just bad code?

Share this post


Link to post
Share on other sites

I don't mean to change the subject or continue the topic when it has been resolved but looking into it I tried a few things out.

I am wondering if this is sloppy coding, or is it an exceptible way to go about it.

The below code would work, but it does not include the

#include <Array.au3>

or

_ArrayDisplay($FileList,"$FileList")

Would any of you more experienced AutoIt ers use the first example or is it just bad code?

The _ArrayDisplay() was only there for debug/demo purposes and is safely discarded once you have it working for you.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

The _ArrayDisplay() was only there for debug/demo purposes and is safely discarded once you have it working for you.

:P

Yea,.. thats right, how did you know? ;)

Share this post


Link to post
Share on other sites

#12 ·  Posted

On 11/28/2007 at 0:05 PM, Zedna said:

Then use FileGetAttrib() to clarify file/directory status.

 

 

$folder="C:\Testing\xyz"
IF FileExist($folder) and StringInStr(FileGetAttrib($folder),"D") Then MsgBox(0,"Folder Found","The folder " & $folder & " exists")

10 years later and post is still helping people. Thanks!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0