Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Exists or not? Can't use dir() 1

Status
Not open for further replies.

Werneck

Technical User
Joined
Aug 5, 2003
Messages
36
Location
BR
Hi,

How I can check if a file exists without using the A97 dir() function? I need to check the existence of a possible new file name while on a loop that already uses dir(). The task is to traverse an hierarchy of folders containing sub folders and files, renaming some.

I got the function bellow but don't how to make it work, may be it needs a reference which I couldn't find.

Function FileExistsFSO(sFile As String) As Boolean
Dim fs As FileSystemObject
FileExistsFSO = True
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(sFile) = False Then FileExistsFSO = False
Set fs = Nothing
End Function

Thanks
 
you need to set a reference to Microsoft Scripting Runtime

PaulF
 
Not sure whether this is an alternative.

Public Function FileExists(filename As String)
On Error GoTo NoSuchFile
If Len((Dir(filename))) < 2 Then
FileExists = False
Else
FileExists = True
End If
Exit Function
NoSuchFile:
FileExists = False
Resume Next
End Function
 
This function checks for a file's existance by using GetAttr() instead of Dir()...
[tt]
Function FileExists(strFile As String) As Boolean
' Comments : Determines if the file exists
' Works for hidden files and folders
' Parameters: strFile - file to check
' Returns : True if the file exists, otherwise false
Dim intAttr As Integer
Dim errNum As Long
On Error Resume Next
'GET THE FILE ATTRIBUTE INSTEAD OF THE LENGTH OF THE FILE NAME
intAttr = GetAttr(strFile)
errNum = Err.Number
FileExists = (Err.Number = 0)
End Function
[/tt]
 
Hi
This function that checks for a file's existance by using GetAttr() instead of Dir() or FileLen() solved my problem.
Thanks you all for the helpfull suggestions!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top