Using VBA to check whether a file in a folder in a certain drive exists.

AshikHusein

Registered User.
Local time
Today, 12:39
Joined
Feb 7, 2003
Messages
147
I am using a batch file to automatically import a certain table which is originally in the TEXT format. The batch file invokes an ACCESS macro which does the actual TransferText procedure.

There are time that the TEXT table may not exist in the required drive and folder. But the windows scheduler is programmed to run the macro to import that table, irrespective of whether it exists or not.

I would like to know what VBA code I could write to check this. I would imagine that it would be a function returning a boolean expression to check whether the particular .txt file exists in the drive. Would appreciate help in the above matter. Thanks
 
Hi,

This is not my code so all credit/thanks should go to I think fuzzygeek!?

'Check to see if a File exists
'
Public Function strfisfile(stPath As String) As Boolean
'Fully qualify stPath
'To check for a file example
' fIsFile("C:\FolderName\FileName.doc")
'
On Error Resume Next
strfisfile = Len(Dir(stPath))
End Function

then use this function as below:

If strfisfile("c:\leads_folder\pool\pool.txt") Then

whatever...

HTH's

Tommy B
 
I have been trying this code today and it always returns as false even if the file exists. Also, do you know know what the following code is for? Thanks for the help.

On Error Resume Next
strfisfile = Len(Dir(stPath))
End Function
 
Public Function Find_This_File(ByVal strFileName As String) As Boolean
On Error GoTo findErr
Dim t_date
t_date = FileDateTime(strFileName)
Find_This_File = True
findErrExit:
Exit Function
findErr:
Find_This_File = False
Resume findErrExit
End Function

This works for me.
 

Users who are viewing this thread

Back
Top Bottom