Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407418
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:44:55+00:00 2026-05-18T05:44:55+00:00

I need to check if a file exists within a directory. The two options

  • 0

I need to check if a file exists within a directory. The two options I thought of are using a regex (vbscript.regexp) to determine if the file is present. The other option being to use the FileSystemObject‘s FileExists method.

The cons of using the regex implies that I will need to loop through every file in the directory and test the regex against each filename. Secondly, a regex approach may be harder to maintain (the regex) in the future. However, I need to test two regex’s since there may be a different version of the same file. For one of the files, there is a description part of the filename which changes frequently(ok, that’s a lie – it changes on some random cycle), so using a regex will assist in matching the files regardless of this part = more robust code.

Using FileExists, I can test for both files simultaneously and depending on the boolean value returned for each check, processing can continue. The related downside for using FileExists is that wildcard matching is not supported, so when considering the filename with a description, this will always need to be updated to reflect the new description = more maintenance headaches. However, it is simpler to grasp and easier to change.

Format of filenames:

Data_Sheet_<yyyymmdd>_D.xlsx // the normal file
Data_Sheet_<yyyymmdd>_D_<some description>.xlsx // the alternate file

The <yyyymmdd> part of the filename will be determined by the current folder the file is located in.

The question: Would it be better to use a regex or a simple FileExists to find files?

I will using the either of the above methods within an Excel vba macro.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:44:56+00:00Added an answer on May 18, 2026 at 5:44 am

    Have you looked at Dir() ? You can pass it wildcards Like my*file.pd? Perhaps it might be a way to compromise. Sure you wouldn’t get the full power of a regex, but you could limit your files and then actually test to see what exists.

    Personally I would use FileExists() rather than looping over a directory and testing every file. If the directory got huge than your loop could take forever. Though if you use Dir() you could potentially limit the number of files you need to iterate over. Plus FileExists() is exactly one line/statement and cleaner… though then you have to make sure the Scripting Runtime is included in your project

    http://techonthenet.com/excel/formulas/dir.php
    http://www.blueclaw-db.com/listbox-directory.htm
    http://www.hobbub.com/vba-vb-vsto/iterating-over-a-directory-with-a-dir-loop-loop-through-directory-vb/

    Second and third links have better examples of iterating over the directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check if a file exists on my server without using cache.
I need to check if a file exists but I don't know the extension.
I need to check if a file is on HDD at a specified location
How can I check if a file exists in a war file? I know
I have a condition where I need to check for the file in another
Is there a single way of detecting if a directory/file/symlink/etc. entity (more generalized) exists?
I'm trying to check if a file exists or not in Tcl, but I
I have an application that will check if a javascript file exists on our
I need to read each file from a directory to parse them individualy when
I am developing file system driver under Windows and i need to check file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.