[VBScript] AppData Folder Removal Example

AoN

Internet Programmer
Joined
Aug 1, 2012
Posts
114
Alrighty, so...I WAS going to make a post asking for help, but I ended up figuring it out myself. Instead, here's a script that you may find help in this relatively unique situation.

A little explination of this script: My work had a program that installed to the "AppData" directory for the individual users, instead of to the "Program Files" of the machine. Well, the program was "removed" from the network, but not from the machines. This script will go through and generate a "path" in the memory for EVERY user on EVERY machine listed in the "computers.txt" and then sends a delete command for those paths. Since the program wasn't used by every user that has logged into the machines, the system will error if the path doesn't actually exist. To avoid the errors, I went ahead and utilized the "On Error Resume Next".

The paths can easily be modified, I made it relatively dynamic, and it does support multiple OS versions, current 7 and XP, but it can be modified for just one or for more.

Any feedback yall have would be appreciated. ^^

Code:
On Error Resume Next
Const ForReading = 1
Const OverWriteExisting = TRUE
Const ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

'*******************************************************************************
' Set this path to the ABSOLUTE location of your list of computer names (one computer name per line)
'*******************************************************************************
Set objFile = objFSO.OpenTextFile("{ABSOLUTE}\computers.txt")
Set WshShell = WScript.CreateObject("WScript.Shell")

Do Until objFile.AtEndOfStream
 strComputer = objFile.ReadLine

'*******************************************************************************
' This will check to see if the computer is even on the network
'*******************************************************************************
 Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strComputer)
 strPingResults = LCase(WshExec.StdOut.ReadAll)
 If InStr(strPingResults, "reply from") Then
'*******************************************************************************
' If the computer is live on the network, let's determine if it is a Windows XP or Windows 7
' Note: It is relatively easy to add additional versions should your network, for some reason, have more
'*******************************************************************************
  Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
  For Each objItem in colItems
   VerBig = Left(objItem.Version,3)
  Next
  Select Case VerBig
   Case "6.1"
    OSystem = "7"
    userPath = "\\" & strComputer & "\c$\Users"
    appPath = "AppData\Local\Apps\2.0"
   Case "5.1"
    OSystem = "XP"
    userPath = "\\" & strComputer & "\d$\Documents and Settings"
    appPath = "Local Settings\Apps\2.0"
   Case Else OSystem = "Invalid OS"
  End Select

'*******************************************************************************
' You and uncomment this line to be prompted with the machines OS before proceeding (made for testing)
'*******************************************************************************
'  Wscript.Echo "The OS version for " & strComputer & " is Win " & OSystem

  sProfile = WshShell.ExpandEnvironmentStrings(userPath)
  sProfileRoot=objFSO.GetFolder(sProfile)
  set objProfileFolder=objFSO.GetFolder(sProfileRoot)
'*******************************************************************************
' Using the "userPath" and "appPath" variable defined by the OS version, the script will delete the folder
'      userPath\*\appPath (the * is only for a single directory level)
'*******************************************************************************
  for each objFolder in objProfileFolder.SubFolders
   sProfile = sProfileRoot & "\" & objFolder.Name & "\" & appPath
   Set FSO = CreateObject("Scripting.FilesystemObject")
   FSO.DeleteFolder(sProfile)
'*******************************************************************************
' This is a troubleshooting set of lines meant to ensure the paths being generated by the script for
'      deletion are what you want
'*******************************************************************************
'   Set QTPfile = FSO.OpenTextFile("D:\Documents and Settings\chris.esbrandt\Desktop\profiles2.txt", ForAppending, True)
'   QTPfile.WriteLine(sProfile & vbCr)
'   QTPfile.Close
  next
'*******************************************************************************
' Else If machine == OFFLINE Then add the machine name to a list of offline machines (again, ABSOLUTE
'      path is needed)
'*******************************************************************************
 Else
  Set FSO = CreateObject("Scripting.FilesystemObject")
  Set QTPfile = FSO.OpenTextFile("{ABSOLUTE}\offlineComputers.txt", ForAppending, True)
  QTPfile.WriteLine(strComputer & vbCr)
  QTPfile.Close
 End If
Loop
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top