View Full Version : Solved: Dir and MkDir
akn112
03-06-2007, 09:32 AM
I'm Having implementing this code. Everytime i run it (no matter if the folder exists or not) It tries and creates the folder. Does anyone know why?
Sub Check_Folder()
On Error GoTo Err_Check_Folder
If Dir("C:\Documents and Settings\185208\Desktop\FAO Report") = "" Then
MsgBox ("Folder Created")
MkDir ("C:\Documents and Settings\185208\Desktop\FAO Report")
End If
Exit_Check_Folder:
Exit Sub
Err_Check_Folder:
MsgBox Err.Description
Resume
End Sub
Sub Check_Folder()
On Error Goto Err_Check_Folder
If Dir("C:\Documents and Settings\185208\Desktop\FAO Report", vbDirectory) = "" Then
MsgBox ("Folder Created")
MkDir ("C:\Documents and Settings\185208\Desktop\FAO Report")
End If
Exit_Check_Folder:
Exit Sub
Err_Check_Folder:
MsgBox Err.Description
Resume
End Sub
I think that'll work
Bob Phillips
03-06-2007, 02:40 PM
If you are going to create it if it doesn't exist, why bother checking
Sub Check_Folder()
On Error Resume Next
MkDir ("C:\Documents and Settings\185208\Desktop\FAO Report")
On Error GoTo Err_Check_Folder
Exit_Check_Folder:
Exit Sub
Err_Check_Folder:
MsgBox Err.Description
Resume
End Sub
Sjefke
03-06-2007, 02:40 PM
I would put the msgbox AFTER the MkDir line - now you tell the user it is created, but it is not - something might go wrong on creation...
mdmackillop
03-06-2007, 03:04 PM
I would put the msgbox AFTER the MkDir line - now you tell the user it is created, but it is not - something might go wrong on creation...
If you did that, the message would show every time, error or not. XLD's code will show the message only if the error occurs.
Sjefke
03-06-2007, 03:38 PM
mdmackillop, I was referring to Moa's code:
If Dir("C:\Documents and Settings\185208\Desktop\FAO Report", vbDirectory) = "" Then
MsgBox ("Folder Created")
MkDir ("C:\Documents and Settings\185208\Desktop\FAO Report")
End If
Obviously not to the Msgbox in the ErrorHandler in any of the postings...
But ok, I didn't point that out, so that was confusing indeed.
XLD's code is a better solution (although maybe not that 'elegant' :-))
Wasn't my code. I just added ", vbDirectory" to answer the question:
I'm Having implementing this code. Everytime i run it (no matter if the folder exists or not) It tries and creates the folder. Does anyone know why?
I am guessing the Dir function is looking for a file named FAO Report and not a folder ubless explicitly told to do so... but I'm not sure.
I would also go with xld's code BTW.
You should use the FolderExists Method to establish if a Folder already exists.
This is from the Help
FolderExists Method
Description
Returns True if a specified folder exists; False if it does not.
Syntax
object.FolderExists(folderspec)
The FolderExists method syntax has these parts:
Part Description object Required. Always the name of a FileSystemObject. folderspec Required. The name of the folder whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the folder isn't expected to exist in the current folder
Don't know about "should" OBP, is there something wrong with Dir function? I guess if you use a FileSystemObject you could use its createFolder method as well but it doesn't seem necessary...
Glen, dir is not normally used for checking the presence of folders, it is normally used to check for the Existence of Files. In the dir regime the GetAttr function is used to check if a path exists.
I thought from akn's first post that he was checking for a "Folder" not a "File".:dunno
Perhaps it is just a wording thing? :)
akn112
03-07-2007, 07:11 AM
OBP:You are right, i am trying to look for a folder and checking for its existance.
I have tried the suggestions the moa has provided and the code works now! Thanks! I will try to use the FolderExists method now as it is more explicit in the method name and programmers analysing my code later on will understand it more easily. Thanks Everyone!
Bob Phillips
03-07-2007, 03:17 PM
Glen, dir is not normally used for checking the presence of folders, it is normally used to check for the Existence of Files. In the dir regime the GetAttr function is used to check if a path exists.
I thought from akn's first post that he was checking for a "Folder" not a "File".:dunno
Perhaps it is just a wording thing? :)
From help
Dir Function
Returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.
Syntax
Dir[(pathname[, attributes])]
The Dir function syntax has these parts:
Part Description pathname Optional. String expression (http://javascript%3Cb%3E%3C/b%3E:hhobj_4.Click%28%29) that specifies a file name ? may include directory or folder, and drive. A zero-length string ("") is returned if pathname is not found. attributes Optional. Constant (http://javascript%3Cb%3E%3C/b%3E:hhobj_5.Click%28%29) or numeric expression (http://javascript%3Cb%3E%3C/b%3E:hhobj_6.Click%28%29), whose sum specifies file attributes. If omitted, returns files that match pathname but have no attributes.
Settings
The attributes argument (http://javascript%3Cb%3E%3C/b%3E:hhobj_7.Click%28%29) settings are:
Constant Value Description
vbNormal 0 (Default) Specifies files with no attributes.
vbReadOnly 1 Specifies read-only files in addition to files with no attributes.
vbHidden 2 Specifies hidden files in addition to files with no attributes.
VbSystem 4 Specifies system files in addition to files with no attributes. Not available on the Macintosh.
vbVolume 8 Specifies volume label; if any other attributed is specified,
vbVolume is ignored. Not available on the Macintosh.
vbDirectory 16 Specifies directories or folders in addition to files with no attributes.
vbAlias 64 Specified file name is an alias. Available only on the Macintosh.
In the dir regime the GetAttr function is used to check if a path exists.
OBP, not sure if I'm using it correctly but GetAttr throws an error if the folder doesn't exist. I think it is only used for checking attributes of a given file or folder.
Still rather use Dir to find a folder and in akn112's case use MkDir and catch the error if folder doesn't exist.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.