Monday, October 30, 2017

To delete all files in the specified path

Function fnDeleteAllFiles_InSpecifiedPath(strPathName)
   On Error Resume Next
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set oFolder = fso.GetFolder(strPathName)
   For Each oFile In oFolder.files
       oFile.Delete True
   Next
   'Delete Folders and sub folders
   Set colSubfolders = oFolder.Subfolders
   On Error Resume Next
   For Each oSubfolder in colSubfolders
fso.DeleteFolder(oSubfolder), True
   Next
   Set fso = Nothing
   Set oFolder = Nothing
   Set colSubfolders = Nothing
   Set oSubfolder = Nothing
If Err.Number <> 0 Then
ErrorNumber =" Error # " & CStr(Err.Number)
ErrorDescription = Err.Description
Call DoneResult("Specified Path '"& strPathName &"' is incorrect", "Not able to delete files and sub folders from the specified path:" & strPathName )
Err.Clear ' Clear the error.
Else
Call DoneResult("All files, Folders and Sub Folders are deleted in '" & strPathName & "'", "All files and sub folders are deleted in the specifed path")
End If
End Function

No comments:

Post a Comment