自动删除 Windows 计算机列表中某个目录中的所有文件

自动删除 Windows 计算机列表中某个目录中的所有文件

我有数百台计算机存在空间问题,最近,我一直对这个列表中的每台计算机执行 PSExecing,并在每台机器上执行“del /s /qc:\DIRECTORY*”,这太荒谬了。

我找到了一个 VB 脚本,它可以查询 ComputerList.txt 文件,我可以使用相关所有计算机的所有主机名的列表来修改该文件,然后再查询另一个 FolderList.txt 文件,我可以在其中列出所有要清空的目录。

但脚本不起作用,它无论如何都会删除目录中的文件夹。当然,我想删除文件夹中的所有项目。而不是删除文件夹本身。

我想要的功能是,VBScript 会查询 ComputerList.txt 文件,其中包含所有要修改的主机名。然后,它会删除 FoldersList.txt 文件中列出的目录中的所有文件。跳过所有正在使用的文件或我的权限无法管理的主机名。如果能将由于权限或离线而被跳过的主机名列表导出到 TXT 文件,以便我可以参考它们,那就太理想了。

如果有人能帮我解决这个问题,那就太好了!谢谢大家。这是我的脚本:

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

相关内容