Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code to check if directory exists

    Word 97 - VBA

    I need to check if a certain directory exists before
    I save a file into that directory.
    If directory doesnt exist - then create & then save file into directory.

    I can only find syntax which searches fileNames.
    Does anyone know the VBA syntax or have code which does this?
    TIA Diana

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: code to check if directory exists

    Diana,

    The FileExists function that Kevin recently posted <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=38271&page=0& view=expanded&sb=5&vc=1#Post38271>here</A> can be used to find folders, as well as files.

    Gary

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Gary

    The code Kevin posted - I ran code to check if directory exists - & doesnt return accurate results.

    I would like to check if directory exists & if doesnt than create directory.

    Diana

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: code to check if directory exists

    Hi Diana,

    One thing that could cause that function to appear to return inaccurate results, would be if the path separator were left off the end of the directory string (sorry, should have mentioned that). The following example does work when I test it:

    Public Sub TestFileExistsFunction()
    If bFileExists("C:TEMP") Then
    MsgBox "The folder exists"
    End If
    End Sub
    '======================
    Public Function bFileExists(strFullName) As Boolean
    bFileExists = CBool(Len(Dir(strFullName)))
    End Function

    If you are including the path separator and it's still giving inaccurate results, post back with some more details, and maybe we can suss it out.

    Regards,
    Gary

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Diana,

    Check out my other post in this thread. I think I've posted what you need.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Diana, using the File System Objects (FSO) the following sample code will do what you want provided that you can set up a reference to Microsoft Scripting Runtime. I am not sure if that can be done in Office 97 but Gary posted ( Here ) that it might be possible.

    <pre>Public fso As New FileSystemObject

    Sub FolderExist()
    Dim fldr As String
    fldr = "C:TestFolder"
    If Not fso.FolderExists(fldr) Then
    fso.CreateFolder (fldr)
    MsgBox fldr & " Created"
    Else
    MsgBox fldr & " already exists"
    End If
    End Sub</pre>


    Andrew C

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Gary,

    Thanks for your confidence in my function. However, Ms. Diana may be encountering a real problem that is fixed in the code below -- a slight oversight. Your suggestion about the final backslash is useful if the second parameter of the Dir function is omitted (which is the case in my original post) Dir() works fine with a naked bottom:

    Dir("c:winnttemp", 16) works fine.

    Second parameter constant for a directory is vbDirectory &H16. Here's a better version: (and a good solution for Diana)

    <pre>TempFolder = Environ("Windir") + "Temp"
    if not FolderExists(tempfolder) then
    MkDir TempFolder
    endif

    Funtion FolderExists(strFolderName) as boolean
    folderexists = cbool(len(dir(strfoldername,vbdirectory)))
    end function
    </pre>

    Since the funtion is defined as boolean, I doubt the call to cbool is even necessary. Compiler (interpreter?) aficionados can set us straight.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Thankyou Kevin

    that worked beautifully,
    the directory I was checking for was
    c:TempIn.

    Your've used these functions in your code
    eg Environ, cbool
    I'm aware of inbuilt functions but not what they are.
    Can you suggest a book/s I can buy which has all these functions.
    Or is there another way I can increase my VBA knowledge of these functions - what do you suggest.
    cheers Diana

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: code to check if directory exists

    The 'cbool' is still necessary (if you're going to set the function up this way):

    The function is defined as boolean, so folderexists "expects" the right side of the expression to evaluate to a boolean; "len(dir(strfoldername,vbdirectory))" by and of itself doesn't evaluate to a boolean (without that cool 'cbool').

    The 'cbool' is a neat way to avoid the lengthier:

    If len(dir(strfoldername,vbdirectory) Then
    folderexists = True
    End If

    (which first line is itself a contraction of:
    If len(dir(strfoldername,vbdirectory)) > 0 Then )

    (BTW I agree with Diana: Environ and cbool were a couple of cool ones to pull out of the hat. 'Environ' sent me scurrying off to the object browser and Help.)

    (Diana - while we're waiting for Kevin to come back <g> - my two tips for finding these things are (1) Object Browser (F2) coupled with VBA Help, and (2) VB & VBA in a Nutshell, by Paul Lomax, O'Reilly Press - great book.)

    Gary

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Diana,

    Glad to know the function worked.

    As far as learning VBA goes, you would greatly benefit from first NOT spending any money just yet, but use the VBA help to discover the available functions and what they do. In the Word VBA help, for example, click the Contents tab then the Visual Basic Language Reference. You'll see several months of learning right there, including the listing of all functions. I have always made it a practice when learning a language to read the language reference -- just to see what goodies you get to use!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Au contraire, mon ami,

    Well, you prompted me to test anyway re: the necessity of cbool(). Follow this reasoning and tell me what you think:

    1. Function is defined as boolean, therefore, only two values will ever be returned: TRUE FALSE

    2. If the Len() function returns zero, zero is defined in VBA (VB) as the numeric equivalent of FALSE

    3. If the Len() function returns non-zero (it'll never be negative), VB interprets >0 as TRUE

    Therefore, cbool is not necessary for the FolderExists function to work properly.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: code to check if directory exists

    Cool! - didn't know that - thanks <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Even though cbool() is not necessary in this case, it sure looks cool. And it will impress your friends at parties!

    It might even prompt fellow posters to delve into the documentation to learn more about VBA!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #14
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Kevin - many thanks - Diana

  15. #15
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Northern, California, USA
    Posts
    1,886
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: code to check if directory exists

    Uhm.. why isn't this working for me? <img src=/S/doh.gif border=0 alt=doh width=15 height=15>!
    <IMG SRC=http://www.wopr.com/w3tuserpics/Kel_sig.gif>
    Moderator:<font color=448800> Pix Place, Internet Explorer</font color=448800>
    <small>www.kvisions.com

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •