Monday, October 30, 2017

to delete the specfied file in specified path

Function fnDeleteFile_InSpecifiedPath(strPathName, strFileName)
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(strPathName)
Result = 0
If Err.Number <> 0 Then
ErrorNumber =" Error # " & CStr(Err.Number)
ErrorDescription = Err.Description
Call DoneResult("Specified Path '"& strPathName &"' is incorrect", "Not able to delete file: " & strFileName & " from " & strPathName)
Err.Clear ' Clear the error.
Exit Function
End If
On Error Resume Next
For Each oFile In oFolder.files
  If strcomp(oFile.name,strFileName,0) = 0 Then
oFile.Delete True
Call DoneResult("The file '" & strFileName & "'  is successfully deleted from '" & strPathName & "'" , "File is successfully deleted from '" & strPathName & "'")
            Result = 1
Exit For
  End If
    Next
If Result = 0 Then
Call DoneResult("The file: '" & strFileName & "' ' is not found in '" & strPathName & "'" , "Not able to delete the file from '" & strPathName& "'")
        Exit Function
End If
   Set fso = Nothing
   Set oFolder = Nothing
End Function

No comments:

Post a Comment