Link to home
Start Free TrialLog in
Avatar of BRPsysadmin
BRPsysadmin

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of jawa29
jawa29
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BRPsysadmin
BRPsysadmin

ASKER

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
    objProcess.Terminate()
Next

' 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
        Err.Clear
        oFSO.DeleteFile(filepath)
        If Err.Number<>0 Then
            WScript.Echo "Cannot delete file - "& filepath &" Err Number:"& Err.Number & " Err Description: " & Err.Description
        End If
    End If
   
Next

'Install Application

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

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

Set oShell = Nothing