Windows 컴퓨터 목록에서 디렉터리의 모든 파일 삭제를 자동화합니다.

Windows 컴퓨터 목록에서 디렉터리의 모든 파일 삭제를 자동화합니다.

공간 문제가 있는 수백 대의 컴퓨터가 있는데 최근에는 이 목록에 있는 각 컴퓨터에 대해 PSExecing을 수행하고 각 컴퓨터에서 "del /s /qc:\DIRECTORY*"를 수행했습니다. 이는 정말 터무니없는 일입니다.

문제가 있는 모든 컴퓨터의 모든 호스트 이름 목록으로 수정할 수 있는 ComputerList.txt 파일을 쿼리하는 VB 스크립트와 모든 디렉터리를 비워 둘 수 있는 또 다른 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

관련 정보