Check file exist vba

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
905
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
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,271
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]
 

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
905
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:

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,543
Messages
5,511,943
Members
408,869
Latest member
MM005

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top