How To Delete Folders In A Remote Computer Using WMI & VB.Net
advertisement
Share This
While working on a Software Tool for my employer using VB.Net, which enables the Desktop Technicians to connect to Remote Computers to apply fixes and carry out routine services I was told to include a functionality to remove Local User Profiles in a Remote Machine which is not being used for more than 90 days.
While I was able to retrieve the list of all User Profiles in the Remote Machine I was stuck in the most important part – Removing the User Data stored in Documents & Settings folder. Apparently there are a lot of ways to remove folders in a remote machine, the most popular being using a VB Script, making use of the Windows Management Instrumentation, I tried and failed several times.
As I was using VB.Net, translating the available VB Script techniques to remove remote folders was a process, always giving me one or other error. I tried translating and using a VB Script given in the Microsoft Scripting Center and that too failed. It didn’t gave me any error, rather it failed in finding all the subfolders for the inside the main directory.
From Windows XP SP2, if you try to remove a folder with contents inside it, Windows will simply ignore your command. It won’t allow you to remove folders unless you remove all the subfolders and files. Also the script provided by Scripting Guys uses a recursive function inside the main, which I was not able to recreate efficiently. So I tried and tried and after a lot of trial and error methods I was successful.
The resulting script provided me the flexibility to remove any folder in a Remote Computer, removing all the subfolders and files inside it. Given below is the code for removing a folder form a Remote Computer. this consists of two functions, one being a recursive function getting list of all the subfolders in the main directory you specified. This is stored in an array and for each of these sub-folders it removes the files inside them, starting at the last entry, and at the end removing the main directory.
Imports System.Management
This is the main function to find and delete the folder and subfolders. Pass the target Directory as value of the Parameter Direcotry
Public Function DeleteFolder(ByVal Directory As String) As Boolean
Dim arrFolders() As String, strSubFolder As String, FolderArray As String()
Dim objWMIService, objFolder As Object, objFile As Object
Dim colFolders As System.Object, colFiles As System.Object
Dim intSize As Integer = 1, intIndex As Integer
Try
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = Directory
intSize = intSize + 1
m_strFolders = Directory
objWMIService = GetObject("winmgmts:\\" & REMOTE_COMPUTER_NAME & "\root\cimv2")
colFolders = objWMIService.ExecQuery ("Associators of {Win32_Directory.Name='" _
& Directory & "'} " _
& "Where AssocClass = Win32_Subdirectory "_
& "role = GroupComponent")
For Each objFolder In colFolders
m_strFolders = m_strFolders & "" & objFolder.Name
GetSubFolders(objFolder.Name, arrFolders)
Next
FolderArray = m_strFolders.Split("")
For intIndex = UBound(FolderArray) To 0 Step -1
strSubFolder = FolderArray(intIndex)
strSubFolder = Replace(strSubFolder, "\", "\\")
colFiles = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strSubFolder & "'")
For Each objFile In colFiles
objFile.Delete()
Next
Next
Return True
Catch ex As Exception
If LTrim(RTrim(ex.Message)) = "Not found" Then ' THE FOLDER IS NOT FOUND IN THE REMOTE MACHINE
Return True
End If
Msgbox ex.message & " " & ex.InnerException.Message
Return False
End Try
End Function
The string REMOTE_COMPUTER_NAMEholds the Remote Computer name. Replace that string with the desired computer name to get connected to. You can specify
.
to get connected with the local computer.
This function works in a recursive manner to get the lsit of all folders and subfolders which are inside the main target directory and stores it in the string m_strFolders, seperated by the character
.
Public Function GetSubFolders(ByVal FolderName As String, ByVal FolderArray() As String) As String()
m_strErrorSource = "GetSubFolders()"
Dim objWMIService, objFolder As Object
Dim colFolders As System.Object
Dim strFolderName As String
Dim intSize As Integer = 1
Try
ReDim Preserve FolderArray(UBound(FolderArray))
intSize = UBound(FolderArray) + 1
objWMIService = GetObject("winmgmts:\\" & REMOTE_COMPUTER_NAME & "\root\cimv2")
colFolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & FolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "role = GroupComponent")
For Each objFolder In colFolders
strFolderName = objFolder.Name.ToString
ReDim Preserve FolderArray(intSize)
m_strFolders = m_strFolders & "" & strFolderName
FolderArray(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders(strFolderName, FolderArray)
Next
Return FolderArray
Catch ex As Exception
FolderArray = Nothing
Msgbox ex.message & " " & ex.InnerException.Message
Return FolderArray
End Try
End Function
Both these functions use a common varibale named m_strFolders which is being used to store the folder names, and is split into individual folder names and stored in the arrray FolderArray. You must declare this as a Global Variable. Another workaround is to declare a Global Array variable and store the folder names in the array, instead os storing the foldernames in the string and then splitting it.
Anyhow I achieved this and thought it would be useful for folks like me who are struggling to find some useful VB.Net code using WMI for System Management.