473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,398 developers and data experts.

Does a File Exist?

ADezii
8,834 Expert 8TB
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One Method to guarantee that a File does in fact exist, is to pass the Absolute Path to the File to a Function which will investigate the Path and return a Boolean Value (True/False) indicating whether or not the File exists. The following code segments will do just that. It accepts a single String Argument representing the Path to the File, if it exists, it returns True, and the User can proceed as normal, if not, a Message Box appears indicating to the User that the File does not exist in the location that was passed to it.
  1. Function Definition
    Expand|Select|Wrap|Line Numbers
    1. Public Function fFileExists(strName As String) As Boolean
    2. Dim strTempString As String
    3.  
    4. On Error Resume Next      'will check for an Error condition shortly in the Function assignment
    5.  
    6. 'Search for the File with 'any' attribute set.
    7. strTempString = Dir$(strName, vbHidden Or vbSystem Or vbArchive Or vbReadOnly)
    8.  
    9. 'If the Length of the FilePath > 0 and no Error condition exists, File exists
    10. fFileExists = ((Len(strTempString) > 0) And (Err.Number = 0))
    11.  
    12. Err.Clear     'Clear the Error Object
    13.  
    14. End Function
    15.  
  2. Call to the Function
    Expand|Select|Wrap|Line Numbers
    1. Const strPathToFile As String = "C:\Windows\System32\Import_1.txt"
    2.  
    3. If fFileExists(strPathToFile) Then
    4.   'normal processing of the File occurs here
    5. Else 
    6.   Dim strBaseFileName As String, strFolderPath As String, Msg As String
    7.  
    8.   strBaseFileName = Right$(strPathToFile, Len(strPathToFile) - InStrRev(strPathToFile, "\"))
    9.   strFolderPath = Left(strPathToFile, InStrRev(strPathToFile, "\"))
    10.  
    11.     Msg = strBaseFileName & " was not found in " & strFolderPath & vbCrLf & vbCrLf
    12.     Msg = Msg & "Please verify your FileName and/or Path, then try again."
    13.       MsgBox Msg, vbCritical, "Invalid Path"
    14.  End If
    15.  
  3. Custom Error Message (Text only) if File and/or Path not found

    Expand|Select|Wrap|Line Numbers
    1. Import_1.txt was not found in C:\Windows\System32\
    2.  
    3. Please verify your FileName and/or Path, then try again.
    4.  
  4. Special considerations
    1. If your File exists on a Network Drive, you would probably want to enhance your Error Handling code since the Path and/or File Name may be valid, but there are a host of additional Network related Errors which may occur.
    2. As Access developers, code similar to that listed above should be SOP (Standard Operating Procedure) anytime we attempt to Open, Close, or Manipulate a File in any manner. We should never assume that the File exists, and the Path to it is valid. It would be a bad programming practice to attempt to operate on it prior to verifying its existence.
Oct 29 '07 #1
2 12572
FishVal
2,653 Expert 2GB
A lazy man should take a look at "Microsoft Scripting Runtime" library.

It has class FileSystemObject, which has method FileExists as well as FolderExists and DriveExists and a bunch of other useful methods and properties.

Regards,
Fish
Oct 29 '07 #2
ADezii
8,834 Expert 8TB
A lazy man should take a look at "Microsoft Scripting Runtime" library.

It has class FileSystemObject, which has method FileExists as well as FolderExists and DriveExists and a bunch of other useful methods and properties.

Regards,
Fish
Excellant point, FishVal, thanks. The only advantages that this 'older' approach may have is that it requires no References, and that it's functional across all Access Versions.
Oct 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
1
by: Poppy | last post by:
Is there anyway in .net to find out if a file exists using its URL as opposed to its physical path ?
3
by: Olivogt | last post by:
Hello, I was just puting an application on the web server but it did not work as usual... - I do develop on my notebook and move released applications to the Web server - both have Sql Server...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
2
by: tino | last post by:
I use File.Exist to check whether a certain image exists. Upon successfull check I want to use the file further but I always get a "The process cannot access the file because it is being used by...
3
by: Mike | last post by:
Thanks for the reply, I have been trying that, but I keep getting the same results. The result I get is that the file exits, when it really doesn't. All my msgbox display twice and I'm not sure...
1
by: Atia Amin | last post by:
Hi, I am a new member. Hello to every one. I am new in ASP.NET area. I wrote an ASP.NET web application which was running ok with my old laptop. Currently I have given a new laptop. Now I copied...
0
by: KingKong07 | last post by:
Hi! I have a problem with publishing my solution. I use “Use fixed naming and single page assemblies” to publish. So I just can update one and one file, this works fine, but today I had to update...
8
by: Anthony Papillion | last post by:
Hello Everyone, I'm writing some code that needs to check if a file exists or not. I'm using the System.IO's File.Exist() method and it's either misreporting or I am not fully understanding how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.