Dealing with the MKDir comand when the folder is alrady there

  • Thread starter Thread starter Yonah Sudwerts
  • Start date Start date
Y

Yonah Sudwerts

I a running MkDir in my script. The problem is that once it runs once, the
folder is created. But the Folder has dynamic names, for each day. How can I
test to see if the folder already exists? So I can avoid running the MkDir
command (and getting a File Access error).

Thanks,
Yoni
 
Hi Yonah,
I a running MkDir in my script. The problem is that once it runs once, the
folder is created. But the Folder has dynamic names, for each day. How can I
test to see if the folder already exists? So I can avoid running the MkDir
command (and getting a File Access error).

If Dir$("x:\path\directory\*.*")<> "" Then MkDir...

Don't know if and how this works if the directory is empty. Another
way would be to simply ignore the error:

On Error Resume Next
MkDir...
On Error Goto 0|Goto Error_Handler
 
I couldnt get the asctual IF condition to work properly. But using the Error
Control does do it. How can I set it up, that the Error Control works only
on this command, I don't want all Erros to Go NExt neccesserally.
Thanks.
Yoni
 
Hi Yonah,

ignore error: On Error Resume Next
jump to a bookmark on error: On Error Goto YourBookmark
switch off error handler: On Error Goto 0
reset error handler (necessary for setting up a new one): On Error
Goto -1

A little sample:

Private Sub Test_Error()
Dim a&, b&, c&

' jump on error to a bookmark
On Error GoTo ERR_HANDLER
Debug.Print "something"

' ignore next error
On Error Resume Next
Err.Raise 13

' jump to a bookmark again
On Error GoTo ERR_HANDLER

a = 1
b = 0
c = a / b

EXIT_:
Debug.Print a, b, c
Exit Sub

ERR_HANDLER:
' For ignoring errors in an error handler it is _
necessary to reset the former handler.
On Error GoTo -1
' ignore next errors
On Error Resume Next
c = a / b
GoTo EXIT_
End Sub
 
I hate to say it, but you lost me..
my code at that point looks like:


Sub something ()
On Error Resume Next
..
..
..
..
If count > 0 Then
'The Following 2 lines Save teh attachment to C:\Telzstone\ Today's Date
strPath = "C:\Telzstone\" & Format(Date, "mm_dd_yyyy") & "\"
MkDir strPath
strFile = strPath & Format(Time, "hh_mm_ss") & ".tiff"
myAtt.Item(1).SaveAsFile strFile
End if
..
..
..
End Sub

How would I change it?

Thanks.
 
Is this solution that I found OK?

strPath = "C:\Telzstone\" & Format(Date, "mm_dd_yyyy") & "\"
On Error Resume Next
MkDir strPath
On Error GoTo 0
strFile = strPath & Format(Item.ReceivedTime, "hh_mm_ss") & ".tiff"
myAtt.Item(1).SaveAsFile strFile

From what I understand the On Error goto 0 resets the handler, so any other
error in my script will stop it from running (which is what I want). I threw
in an error on purpose a little later on and the script did fail, which I
took to mean that my setup is OK.

Just looking for some confirmation.

Thanks.
 
strPath = "C:\Telzstone\" & Format(Date, "mm_dd_yyyy") & "\"
On Error Resume Next
MkDir strPath
On Error GoTo 0
strFile = strPath & Format(Item.ReceivedTime, "hh_mm_ss") & ".tiff"
myAtt.Item(1).SaveAsFile strFile

From what I understand the On Error goto 0 resets the handler, so any other
error in my script will stop it from running (which is what I want).


In this case that´s ok.
 
Hi Yonah,
I couldnt get the asctual IF condition to work properly.

I posted a correction right after this - it must be:

If Dir$("x:\path\directory\*.*")= "" Then MkDir...

Has to be "=" said:
But using the Error
Control does do it. How can I set it up, that the Error Control works only
on this command, I don't want all Erros to Go NExt neccesserally.

On Error Resume Next
MkDir...
On Error Goto 0
 
That didn't do it either, but Its OK, I got the error control to work
properly and that is good enough for me.

Thanks,
Yoni
 
Back
Top