473,372 Members | 1,089 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,372 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 12564
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.