2

I have hundreds of computers with space issues, and lately, I've just been PSExecing into each computer on this list, and performing a "del /s /q c:\DIRECTORY*" on each machine, which is just ridiculous.

I found a VB script that queries a ComputerList.txt file that I could modify with a list of all the hostnames of all the computers in question, and then another FolderList.txt where I could list all the directories to empty.

Except the script isn't working and it deletes a folder for a directory anyhow. Which of course, I want to delete all items in a folder. Not delete the folder itself.

The functionality that I would like is, that the VBScript would query the ComputerList.txt file, which has all the Hostnames to modify. Then it would delete all the files in the directories listed in the FoldersList.txt file. Skipping all files that are in use, or Hostnames that my rights don't administer. It would be ideal if a list of Hostnames that were skipped due to rights or being offline would be exported to a TXT file so I can refer back to them.

If anyone could help me with this, that would be awesome! Thanks everyone. Here's the script I have:

Option Explicit
Const strFolderList = "C:\Scripts\FolderList.txt"
Const strComputers = "C:\Scripts\ComputerList.txt"
Dim objFSO, inFile, ComputerList, objDictionary, strFolderName, colfolders, intSize
Dim arrFolders(), objWMIService, intKey, Item, colSubfolders
intSize = 0

Set objFSO = CreateObject("scripting.filesystemobject")
Set inFile = objFSO.OpenTextFile(strFolderList,1)
Set ComputerList = objFSO.OpenTextFile(strComputers,1)
Set objDictionary = CreateObject("Scripting.Dictionary")
'---------Read folderlist into an Dictionary Array---------
intkey = 1
Do Until inFile.AtEndOfStream
    objDictionary.Add intKey, inFile.ReadLine
    intKey = intKey + 1
Loop
inFile.Close
'-----------------------------------
'----Read computerlist line by line and call FileDelete
Do Until ComputerList.AtEndOfStream
    Call FileDelete(computerlist.ReadLine)
    WScript.Echo "Done."
Loop
ComputerList.Close


'-----Uses the computer name above and connects to WMI 
Sub FileDelete(strComputer)
 Set objWMIService = GetObject("winmgmts:" _ 
  & "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _ 
  & strComputer & "\root\cimv2")
'---loop through the dictionary array and delete folders
    For Each Item In objDictionary.Items
        strFolderName = Item
        Set colSubfolders = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
     & "Where AssocClass = Win32_Subdirectory " _
     & "ResultRole = PartComponent")

        ReDim Preserve arrFolders(intSize)
        arrFolders(intSize) = strFolderName
        intSize = intSize + 1
        On Error Resume Next 
        For Each objFolder in colSubfolders
            'Folder does not exist
            If Hex(Err.Number) = 80041002 Then 
                Err.Clear
             WScript.Echo strFolderName & " does not exist."
            Else
                'folder exists
                GetSubFolders strFolderName
            End If
        Next

        For i = Ubound(arrFolders) to 0 Step -1
            strFolder = arrFolders(i)
            strFolder = Replace(strFolder, "\", "\\")
            Set colFolders = objWMIService.ExecQuery _
             ("Select * from Win32_Directory where Name = '" & strFolder & "'")

            For Each objFolder in colFolders
             errResults = objFolder.Delete
            Next
        Next
    Next
End Sub

Sub GetSubFolders(strFolderName)
 Set colSubfolders2 = objWMIService.ExecQuery _
  ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
  & "Where AssocClass = Win32_Subdirectory " _
  & "ResultRole = PartComponent")

 For Each objFolder2 in colSubfolders2
  strFolderName = objFolder2.Name
  ReDim Preserve arrFolders(intSize)
  arrFolders(intSize) = strFolderName
  intSize = intSize + 1
  GetSubFolders strFolderName
 Next
End Sub

0 Answers0