×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Checking if a file exists.

Checking if a file exists.

Checking if a file exists.

(OP)
I have this SUB to look if a file exists.  If the file exists it's supposed to do nothing, but if it doesn't exists the file is created.  The problem is that if the file already exists the following error occurs: "Path not found."  Of course, the file "X" does exists in "c:\".

Sub test()
    If Dir("c:\X\ProjsB") = "" Then
        MkDir "c:\X\ProjsB"
    End If
End Sub

RE: Checking if a file exists.

mtroche,

I'm assuming you mean folders (directories) not files.  Your code works for me if the path c:\X already exists (and the optional attributes parameter of the Dir function is set to vbDirectory).  Otherwise, the MkDir call throws the "Path not found" error.  In other words, MkDir can only create a directory one level down from an existing directory.  It cannot create all the missing subdirectories in the path parameter in one go.

If c:\X doesn't exist, then this works:

CODE

Sub test()
    If Dir("c:\X\ProjsB", vbDirectory) = "" Then
        MkDir "c:\X"
        MkDir "c:\X\ProjsB"
    End If
End Sub

Instead of using the VBA Dir & MkDir commands, I recommend using the FileSystemObject for file & folder manipulations.  Here is a generalized function that will create a path an arbitrary number of levels deep.  It returns True or False indicating success or not.

CODE

Function CreateFilePath(ByVal FPath As String) As Boolean
Dim FSO As FileSystemObject
Dim ParsedPath As Variant
Dim TempPath As String
Dim i As Long

   On Error Resume Next
   Set FSO = New FileSystemObject
   ParsedPath = Split(FPath, "\")
   TempPath = ParsedPath(0)
   For i = 1 To UBound(ParsedPath)
     TempPath = TempPath & "\" & ParsedPath(i)
     FSO.CreateFolder (TempPath)
   Next i
   CreateFilePath = FSO.FolderExists(FPath)
   Set FSO = Nothing
   
End Function
This function requires that a reference to the Microsoft Scripting Runtime be set.  

Hope this helps.  If I misunderstood something, post back.

Regards,
Mike

RE: Checking if a file exists.

I once found this bit of code to establish if a file exists and have used it ever since.


Function FileExists%(filename$)

   Dim f%
   'trap any errors that may occur
   On Error Resume Next
   'get a free file handle
   f% = FreeFile
   Open filename$ For Input As #f%
   Close #f%
   FileExists% = Not (Err <> 0)

End Function

RE: Checking if a file exists.

Staying on the FileSystemObject bandwagon, the following will determine if a file exists:

CODE

Set FSO = New FileSystemOBject
If FSO.FileExists(FileSpec) Then ...
where FileSpec is the path + filename you are checking.


Regards,
Mike

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

White Paper - Considerations for choosing a 3D printing technology
The adoption of 3D printing into major companies’ product development life cycles is a testament to the technology’s incredible benefits to consumers, designers, engineers and manufacturers. While traditional production methods have limitations in manufacturability, 3D printing provides unparalleled design freedom due to the additive method of building parts layer by layer. Download Now
White Paper - Are all 3D printed metals the same?
With so many different 3D printing metal systems and processes today, how can you know exactly what you’re getting? Today, there are several different methods for manufacturing 3D printed metal components with their own strengths and limits. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close