Monday, August 11, 2014

Deleting Folders & Subfolders using VBscript


Set folder = fso.GetFolder("C:\Program Files\Test")
' delete all files in root folder
for each f in folder.Files
   On Error Resume Next
   name = f.name
   f.Delete True
   
   On Error GoTo 0
Next

' delete all subfolders and files
For Each f In folder.SubFolders
   On Error Resume Next
   name = f.name
   f.Delete True
   
   On Error GoTo 0
Next

Deleting Appdata file for all the users

Dim FSfolder 
Dim subfolder 
Dim i 


set objshell = CreateObject("Wscript.shell") 
Set FSO = CreateObject("Scripting.FileSystemObject")
Profile = strSysDrive & "C:\Users"
Set FSfolder = FSO.GetFolder(Profile) 'getting the user profile folders





'starting of the loop to delete the HKCUs


For Each subfolder In FSfolder.SubFolders


   If (subfolder.Name <> "All Users" And  subfolder.Name <> "Default User"_
   and subfolder.Name <> "Public" and subfolder.Name <> "Default") Then


 folder1=Profile & "\" & subfolder.Name & "\AppData\Roaming\Quest Software\Test"

DeleteThisFolder(folder1)

  end if


Next 


Function DeleteThisFolder(FolderName)


    If FSO.FolderExists(FolderName) Then
       On Error Resume Next
       fso.DeleteFile(Profile & "\" & subfolder.Name & "\AppData\Roaming\Quest Software\Test\sample.lnk")
    End If

End Function