Discussion Locked

How to check if a File Exists

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Hello, I am just starting to get into VBA and have come across the need to write some VB code that checks for a file and then needs to delete that file. I have been reading up on FSO but I must be missing something as I can not get it to work.

7 replies

by farzanfar

Public Function FExists(OrigFile As String)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FExists = fs.fileexists(OrigFile)
End Function

by farzanfar

Try This :
Public Function FileExists(sFullPath As String) As Boolean
Dim oFile As New Scripting.FileSystemObject
FileExists = oFile.FileExists(sFullPath)
End Function

by salvatore.doria

maybe you have some script blocking application running, so that fso doesn't work. why don't you try this:

Dim FileToDelete as string
FileToDelete="C\path1\subpath2\..\file.ext"
if dir(FileToDelete)<>"" then
Kill FileToDelete
end if

Note: the variable "FileToDelete" contains the complete path of the file you want to delete.

This discussion is closed to new replies.