Saturday 29 August 2015

VBS Prog to delete Folder/Subfolders/All Content

' This script will delete a folder and its content along with subfolders data..
''Note: System defined folders are not deleted.


Set fs=CreateObject("scripting.filesystemobject")
On Error Resume Next
pat=InputBox("Please enter the folder path to delete all the files/subfolders" & vbCrLf & "Eg: C:\Documents and Settings\vgopi\Desktop\ImagesFold", "Enter Folder path here..")

'pat="C:\Documents and Settings\vgopi\Desktop\Perso\New Folder\"
'pat="C:\DOCUME~1\vgopi\LOCALS~1\Temp"    '' "%temp%
'pat= "C:\Documents and Settings\vgopi\Recent"


If pat="" Or IsEmpty(pat) Then
    '' Pat folder path is routed to the following
    pat="C:\Documents and Settings\vgopi\Local Settings\Temporary Internet Files\"
Else
    pat=pat & "\"    '' used to avoid restricted error message..
End If
'MsgBox pat
Set fold= fs.GetFolder(pat)
Set fil=fold.Files
For Each fl In fil
    fl.Delete
Next

Set fld=fold.SubFolders
For Each fd In fld
    Call DeleteSubFolders(fd.path)
    fd.Delete(True)
Next
If fs.FolderExists(pat) Then
    MsgBox "done"
    fold.Delete(True)
End If
Set fs= Nothing
''To delete subfolders and its content..
Function DeleteSubFolders(foldPath)
    Set fso1=CreateObject("Scripting.FileSystemObject")
    Set sfold=fso1.GetFolder(foldPath)
    Call DeleteAllFiles(sfold.Path)
    Set subFold=sfold.SubFolders
'    If subfold.Count<>0 Then
        For Each sbfd In subFold
            Call DeleteSubFolders(sbfd.Path)
            sbfd.Delete(True)            
        Next
'    End If
    Set fso1= Nothing
End Function

Function DeleteAllFiles(Sfiles)    
    Set fso2=CreateObject("Scripting.FileSystemObject")
    Set sfold=fso2.GetFolder(Sfiles)
    Set allFiles=sfold.Files
    For Each fl In allFiles
        fl.Delete(True)
    Next
    Set fso2=Nothing
End Function

No comments:

Post a Comment