
スペースの問題を抱えたコンピューターが何百台もあり、最近、このリストにある各コンピューターに PSExec を実行して、各マシンで「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