Check file exist vba

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,075
Office Version
  1. 2019
Platform
  1. Windows
I'm using MKDir to create a file in a folder. When creating a file, the file name is saved with a random generated 16-digits Alpha-Numeric (i.e. DS1O-Q7LN-X7MP-EJA8) name. The code below checks for a specific file, however How can i check if there is a 16-digits Alpha-Numeric file exist?

Code:
Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You want to check if any 16 character filename exists in a given folder?

Code:
[color=darkblue]Function[/color] FileExists(FilePath [color=darkblue]As[/color] [color=darkblue]String[/color]) [color=darkblue]As[/color] [color=darkblue]Boolean[/color]
    
    [color=darkblue]If[/color] Right(FilePath, 1) <> "\" [color=darkblue]Then[/color] FilePath = FilePath & "\"
    
    FileExists = Len(Dir(FilePath & "????-????-????-????.*")) > 0
    
[color=darkblue]End[/color] [color=darkblue]Function[/color]
 
Upvote 0
If a file exist in the described format above, How can I insert the file name in cell? I tried the code below, but it not entering the correct file name.

Code:
Range("A1") = Len((Dir(OPath) & "????-????-????-????.*"))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top