VBScript to enumerate folders and delete a file if it exists

I am installing Symantec Endpoint Protection 11, and doing so there is a problem with the outlook add-in. Symantec recommends deleting C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\extend.dat
I'm weak in scripting so I'm hoping someone out there can help with a script to do the following:
1. Close outlook
2. Enumerate all the "username" folders.
3. Delete any extend.dat files that exist.
4. Install the setup.exe

Or if there is a better way to do this, that'd be great too.
Who is Participating?
Hi BRPsysadmin

Hopefully the VBScript below will help with your request. All you need to do is alter the setup.exe path (sPathToSetup) to the UNC or local path where you will install from.


' Set the WMIService Object and retrieve all Processes called outlook.exe
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery("Select * from Win32_Process Where Name = 'Outlook.exe'")

' Terminate all processes called outlook.exe
For Each oProcess in colProcessList

'Set the Documents and Settings directory and the path to the setup.exe file
sSourceDir = "C:\Documents and Settings"
sPathToSetup = "setup.exe /S"

If Right(sSourceDir,1) <> "\" Then
	sSourceDir = sSourceDir & "\"
End If

' Set the Folder object to the source directory
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sSourceDir)
Set colSubfolders = oFolder.Subfolders

' Enumerate through the subdirectories and look for the extend.dat file, delete if found
For Each oSubfolder in colSubfolders
	If oFSO.FileExists(sSourceDir & oSubfolder.Name & "\Local Settings\Application Data\Microsoft\Outlook\extend.dat") Then
		oFSO.DeleteFile sSourceDir & oSubfolder.Name & "\Local Settings\Application Data\Microsoft\Outlook\extend.dat",True
	End if

' Run the setup command
Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run sPathToSetup,,True

Open in new window

BRPsysadminAuthor Commented:
I will test and get back to you
Thanks in advance
Please try the following..
Error handling included for file deletion and installation included

On Error Resume Next
' Instantiate the various objects

On Error Resume Next
' Instantiate the various objects

strComputer = "."
MyApp = """C:\MyApplication\Setup.exe"" /Q" 'change only the path to setup file leave the remaining

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oWSH    = CreateObject("WScript.Shell")
Set oFSO    = CreateObject("Scripting.FileSystemObject")

'Terminate outlook

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'outlook.exe'")

For Each objProcess in colProcessList

' Get the Allusers profile folder path first and from this determine profiles parent folder
sAllUsersProfile    = oWSH.ExpandEnvironmentStrings("%ALLUSERSPROFILE%")
Set oAllUsersFolder = oFSO.GetFolder(sAllUsersProfile)
sProfilesRoot       = oAllUsersFOlder.ParentFolder

' Now enumerate all existing user profile folders

Set oFolder            = oFSO.GetFolder(sProfilesRoot)
Set colFSOSubfolders   = oFolder.Subfolders

' Now go through each existing user profile folder looking for the designated folder to delete

For Each objSubfolder In colFSOSubfolders
    filepath=objSubFolder.Path & "\Local Settings\Application Data\Microsoft\Outlook\extend.dat"
    'WScript.Echo filepath
    If oFSO.FileExists(filepath) Then
        If Err.Number<>0 Then
            WScript.Echo "Cannot delete file - "& filepath &" Err Number:"& Err.Number & " Err Description: " & Err.Description
        End If
    End If

'Install Application

Set oShell = CreateObject("WScript.Shell")
i = 0
i = oShell.Run(MyApp, 1 ,True)

If i=0 Then
      WScript.Echo "Application successfully installed"
      WScript.Echo "Application installation error, please check"
End If

Set oShell = Nothing

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.