I feel bad about this.... but.... Matty, use the
Dir() function to check if a folder or file exists so you don't need to use error checking* or changing your application path
* it still isn't totally 100% fail proof of course, but you know what I mean
code:
Public Function DoesFolderExist(strPath As String, Optional blnCreateFolder As Boolean = False) As Boolean
Dim AnyFolder As Long
AnyFolder = vbDirectory Or vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
If Dir(strPath, AnyFolder) <> "" Then
DoesFolderExist = True
Else
If blnCreateFolder Then
MkDir strPath
End If
DoesFolderExist = False
End If
End Function
Public Function DoesFileExist(strFile As String, Optional blnCreateFile As Boolean = False) As Boolean
Dim AnyFile As Long
AnyFile = vbReadOnly Or vbHidden Or vbSystem Or vbArchive Or vbNormal
If Dir(strFile, AnyFile) <> "" Then
DoesFileExist = True
Else
If blnCreateFile Then
Open strFile For Output As #1
Close #1
End If
DoesFileExist = False
End If
End Function
IMPORTANT: I suddenly remembered why I never used Dir() for this myself (but the windows API's instead): There is a small, but important, 'twitch' in the Dir() function and the vbDirectory attribute usage (this goes for many programming languages btw, not only VB). Hence, the above "DoesFolderExist" procedure isn't 100% "bug" free and the ones below should be used instead. Sorry for this late, but important, addition...
Because of the nature of the attributes of files and directories in Windows, there can be false positives returned when searching for a directory. eg: Dir() will also return a
file name (if the name matches of course), even if the vbDirectory attribute is given as a mandatory attribute to have.
To overcome this, you can make sure that the given path ends with "\". If the path exists the Dir() function will return the special
file "." (meaning current directory), if not it will return an empty string "", and no false positive will be given anymore.
BUT! by adding the "\" at the end, you actually force Dir() to use the given variable as 'a path name'. If there is that file again with the same name as the last subdir in that path you wanna check, it will not produce a false positive anymore, but a runtime error 52. This is because there was a name conflict when looking up the path (which thus actually wasn't a valid path for Windows)...
Thus, to compensate for this, we need to understand how files on a computer work and how to use "special" filenames. To make it short (you can read detailed stuff about this on the net), you can/should use the special filename "NUL".
With this, we can check for the NUL-file/path. An added bonus to this is that we don't need to specify the other attributes anymore (except for vbDirectory of course) and that invalid characters (like , ; : / etc.) don't trigger an error either!
We still need to check for false positives though, but the name conflicts will not occur anymore...
code:
' "Byval" because we are going to change the variable,
' and we don't want the original passing variable to
' change with it...
Function DoesFolderExist(ByVal strPath As String) As Boolean
' Set the default returning value
DoesFolderExist = False
' Make sure we start with a clean file/path name
If Right$(strPath, 1) = "\" Then strPath = Left$(strPath, Len(strPath) - 1)
' Check for a matching name
If Dir(strPath & "\nul", vbDirectory) = "nul" Then
' Compensate for false positives (matching file names)
If GetAttr(strPath) And vbDirectory Then
' We truely have an existing directory at our hand
DoesFolderExist = True
End If
End If
End Function
Second important note: I left out the creation of the directory because it also isn't as strait forward as simply stating
MkDir strPath. For starters, each dir above the last subdir must exist before you can create that last subdit. eg: if "C:\MyDir" doesn't exist, you can not do
MkDir "C:\MyDir\SubDir".
Last note: simply use the Windows API, life is much easier (and in this case faster and shorter) with that...
PS: The "DoesFileExist" procedure doesn't suffer from all this because, vice versa, files don't have the vbDirectory attribute in the first place and thus a directory will never be returned, even if the name matches a directory! But again, creating a file isn't as strait forward as it is originally stated; there are lots of errors which could occur. And therefore, the DoesFileExist() function can be reduced to (following the same NUL-usage to compensate for bad characters):
code:
Public Function DoesFileExist(strFile As String) As Boolean
If Dir(strFile & "\nul") = "nul" Then
DoesFileExist = (GetAttr(strFile) And (vbDirectory Or vbVolume)) = 0
End If
End Function
Personal note: the usage of the NUL-file/path in these ways is something I nowhere found on the net, it isn't even mentionned on so called prof, advanced or hardcore VB sites. Instead all you find are half-arsed (excuse for the wording) copy/pasted sources. I wonder how many actually know about this :/
---------------------------------------------------------------------------------------------
Anyways, to come back to the subject....
quote:
Originally posted by dylan!
ok ive study lots in the last couple of days and if learnt wuite a bit and i think ive read over 50 pages of tutorials on it on the internet and i was just wondering one small and simple question
ok I want to create a folder and file on the click of a button but i was wondering what i would put to create it?????
e.g
Create "C:\Program Files\Program\Info.txt"
but i know create isnt a valid command and i was wondering what i could put to create it
it isn't so simply though.... There are lots of things to do and to keep in mind when doing something like that.
* What do you want to do if the file already exists? Overwrite it? Append to it?
* What do you want to do when the directory (or subdirectories) don't exist? Create them?
* What if the file and/or directories do exist, but can't be overwritten?
* What kind of data are you going to write to the file? Binary? Text?
* Do you want the file to be locked for writing by another process while you are writing to it?
* Do you want the file to be locked for reading by another process while you are writing to it?
* etc...
But basically, disregarding all those questions it goes like:
code:
Open "C:\Program Files\Program\Info.txt" For Output As #1
Print #1, "some line"
Print #1, "some other line"
Print #1, "more lines"
Close #1
This creates/overwrites the file, without locking it, and write ascii text to it, while assuming the directory exists and is accessible and without checking for disk quota or other errors that might occur.
As said before, use your Visual Basic Documentation which is installed when you installed Visual Basic by pressing <F1>.... Or you can lookup that same documentation online by going to the MSDN Library:
http://msdn.microsoft.com/library/default.asp?url...html/vb6anchor.asp
eg:
The specific docs for the used statements and commands in the above example:
*
Open statement
*
Print # statement
*
Close statement
Learn to use the Visual Basic documentation and/or MSDN Library