自動從 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

相關內容