What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Need VB help(Visual Basic)

Need VB help(Visual Basic)
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Need VB help(Visual Basic)
:$ 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.... :P


quote:
Originally posted by dylan!
ok ive study lots in the last couple of days and if learnt wuite a bit:P and i think ive read over 50 pages of tutorials on it on the internet:P 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 ;)

This post was edited on 09-14-2005 at 03:43 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
08-25-2005 03:49 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 07:26 AM
RE: Need VB help(Visual Basic) - by segosa on 08-22-2005 at 07:33 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 07:34 AM
RE: Need VB help(Visual Basic) - by Dempsey on 08-22-2005 at 07:43 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 07:48 AM
RE: Need VB help(Visual Basic) - by Dempsey on 08-22-2005 at 07:50 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 07:55 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 08:26 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 08:30 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 08:36 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 08:39 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 08:47 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 09:13 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 09:22 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 09:24 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 09:33 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-22-2005 at 09:48 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 10:01 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 10:10 AM
RE: Need VB help(Visual Basic) - by CookieRevised on 08-22-2005 at 11:05 AM
RE: Need VB help(Visual Basic) - by spokes on 08-22-2005 at 11:13 AM
RE: Need VB help(Visual Basic) - by CookieRevised on 08-22-2005 at 11:44 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-25-2005 at 12:49 AM
RE: Need VB help(Visual Basic) - by matty on 08-25-2005 at 01:07 AM
RE: Need VB help(Visual Basic) - by CookieRevised on 08-25-2005 at 03:49 AM
RE: Need VB help(Visual Basic) - by dylan! on 08-25-2005 at 10:50 PM
RE: Need VB help(Visual Basic) - by CookieRevised on 08-25-2005 at 11:53 PM
RE: Need VB help(Visual Basic) - by dylan! on 09-11-2005 at 08:18 AM
RE: Need VB help(Visual Basic) - by spokes on 09-11-2005 at 08:32 AM
RE: Need VB help(Visual Basic) - by dylan! on 09-11-2005 at 09:03 AM
RE: Need VB help(Visual Basic) - by segosa on 09-11-2005 at 10:12 AM
RE: Need VB help(Visual Basic) - by ShawnZ on 09-11-2005 at 10:15 AM
RE: Need VB help(Visual Basic) - by mad_onion on 09-11-2005 at 09:01 PM
RE: RE: Need VB help(Visual Basic) - by segosa on 09-12-2005 at 06:48 AM
RE: Need VB help(Visual Basic) - by prashker on 09-11-2005 at 09:38 PM
RE: Need VB help(Visual Basic) - by mad_onion on 09-12-2005 at 06:15 PM
RE: RE: Need VB help(Visual Basic) - by CookieRevised on 09-14-2005 at 03:53 AM
RE: Need VB help(Visual Basic) - by dylan! on 10-09-2005 at 05:57 AM
RE: Need VB help(Visual Basic) - by CookieRevised on 10-09-2005 at 01:14 PM
RE: Need VB help(Visual Basic) - by dylan! on 10-09-2005 at 10:36 PM
RE: Need VB help(Visual Basic) - by Ezra on 10-09-2005 at 11:17 PM
RE: Need VB help(Visual Basic) - by dylan! on 10-09-2005 at 11:24 PM
RE: Need VB help(Visual Basic) - by surfichris on 10-09-2005 at 11:50 PM
RE: Need VB help(Visual Basic) - by dylan! on 10-09-2005 at 11:53 PM
RE: Need VB help(Visual Basic) - by CookieRevised on 10-10-2005 at 12:13 AM
RE: Need VB help(Visual Basic) - by dylan! on 10-10-2005 at 07:07 PM
RE: Need VB help(Visual Basic) - by Ezra on 10-10-2005 at 07:31 PM
RE: Need VB help(Visual Basic) - by dylan! on 10-22-2005 at 05:49 AM
RE: Need VB help(Visual Basic) - by CookieRevised on 10-22-2005 at 02:05 PM
RE: Need VB help(Visual Basic) - by ShawnZ on 10-22-2005 at 02:53 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On