Jump to content

Unusual Response From Dircreate.


Recommended Posts

Code below creates a folder. I then create it again. It creates on the first call but on the second neither the @error or $status variables return anything to say the directory is already created. I can live with that, just curious as to what is going on?

local $status
$status = DirCreate("C:\AAA")
msgbox(0,"","@error=" & @error)
if $status = 0 Then
    MsgBox(0, "DirCreate error.", "Perhaps it already exists?")
Else
    MsgBox(0, "DirCreate.", "Yes! I created it.")
EndIf
$status = DirCreate("C:\AAA")
msgbox(0,"","@error=" & @error)
if $status = 0 Then
    MsgBox(0, "DirCreate error.", "Perhaps it already exists?")
Else
    MsgBox(0, "DirCreate.", "And for good measure, I created it again! ???")
EndIf
Link to comment
Share on other sites

  • Moderators

Code below creates a folder. I then create it again. It creates on the first call but on the second neither the @error or $status variables return anything to say the directory is already created. I can live with that, just curious as to what is going on?

local $status
$status = DirCreate("C:\AAA")
msgbox(0,"","@error=" & @error)
if $status = 0 Then
    MsgBox(0, "DirCreate error.", "Perhaps it already exists?")
Else
    MsgBox(0, "DirCreate.", "Yes! I created it.")
EndIf
$status = DirCreate("C:\AAA")
msgbox(0,"","@error=" & @error)
if $status = 0 Then
    MsgBox(0, "DirCreate error.", "Perhaps it already exists?")
Else
    MsgBox(0, "DirCreate.", "And for good measure, I created it again! ???")
EndIf
That's because it's creating it again, there is no error.
Do
    DirCreate('C:\AAA')
    Sleep(10)
Until FileExists('C:\AAA')
MsgBox(64, 'Done', 'Your file was created')
Try that....

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

That's because it's creating it again, there is no error.

Do
    DirCreate('C:\AAA')
    Sleep(10)
Until FileExists('C:\AAA')
MsgBox(64, 'Done', 'Your file was created')
Try that....
Let me see if I got this right. If the folder exists and you fill it with files and do the DirCreate again, you actually lose everything, no warning, nothing, history? :)

I must try that in the morning.

Link to comment
Share on other sites

  • Moderators

Let me see if I got this right. If the folder exists and you fill it with files and do the DirCreate again, you actually lose everything, no warning, nothing, history? :)

I must try that in the morning.

I phrased that wrong, it shouldn't overwrite anything (sub folders/files). But, I always take the safe approach anyway... I don't do a function if it isn't needed kind of practice
Local $Directory = 'AAA'
Local $Path = @HomeDrive & '\' & $Directory
If Not FileExists($Path) Then
    Do
        DirCreate($Path)
        Sleep(10)
    Until FileExists($Path)
    MsgBox(64, 'Created', 'Directory Created')
Else
    MsgBox(0, 'Error', 'Directory already exists')
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

It looks as both of you need some assurance with DirCreate? Let me try to cast the doubts away.

Checking the @error macro is a waste of time as DirCreate does not support it !!! It supports boolean methods to check on success. DirCreate will result true if the directory is created or the directory already exists. It will result false if a extensionless file exists of that name or the directory creation fails (no directory exists).

Here is an example that shows the boolean method of checking:

; This is true if directory is created or already exists
If DirCreate("C:\AAA") Then
    MsgBox(0, '', 'Directory is created or already exists')
EndIf

; This is true if a file or directory exists with the pathname
If FileExists("C:\AAA") Then
    MsgBox(0, @ScriptName, 'Either a file or directory exists by that pathname')
; Check the attribute
    $attribute = FileGetAttrib("C:\AAA")
; This is true if it is a directory
    If StringInStr($attribute, 'D') Then
        MsgBox(0, @ScriptName, 'Yes, it is a directory')
    EndIf
EndIf

The 1st If block above should be enough for most without any fancy loops etc. to ensure success. You can always have alternative code using Else within the If block if it does fail.

If you want to immediately check for a False condition, then use Not:

If Not FileExists("C:\AAA") Then

And you will not lose the contents if you use DirCreate again.

:)

Edited by MHz
Link to comment
Share on other sites

  • Moderators

It looks as both of you need some assurance with DirCreate? Let me try to cast the doubts away.

Checking the @error macro is a waste of time as DirCreate does not support it !!! It supports boolean methods to check on success. DirCreate will result true if the directory is created or the directory already exists. It will result false if a extensionless file exists of that name or the directory creation fails (no directory exists).

Here is an example that shows the boolean method of checking:

; This is true if directory is created or already exists
If DirCreate("C:\AAA") Then
    MsgBox(0, '', 'Directory is created or already exists')
EndIf

; This is true if a file or directory exists with the pathname
If FileExists("C:\AAA") Then
    MsgBox(0, @ScriptName, 'Either a file or directory exists by that pathname')
; Check the attribute
    $attribute = FileGetAttrib("C:\AAA")
; This is true if it is a directory
    If StringInStr($attribute, 'D') Then
        MsgBox(0, @ScriptName, 'Yes, it is a directory')
    EndIf
EndIf

The 1st If block above should be enough for most without any fancy loops etc. to ensure success. You can always have alternative code using Else within the If block if it does fail.

If you want to immediately check for a False condition, then use Not:

If Not FileExists("C:\AAA") Then

And you will not lose the contents if you use DirCreate again.

:mellow:

Your so cool!! :)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

It will also return false if you give it an invalid pathname containing illegal characters.

As I stated which covers the above:

or the directory creation fails

AutoIt will not make up for poor code. You have to create good code. Explorer dongs you :) also if you try to use illegal characters.

Link to comment
Share on other sites

As I stated which covers the above:

AutoIt will not make up for poor code. You have to create good code. Explorer dongs you :) also if you try to use illegal characters.

Explorer dongs me. Period. :mellow: Edited by Peter Hamilton-Scott
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...