Solved

VBScript to enumerate folders and delete a file if it exists

Posted on 2011-03-09
3
734 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:BRPsysadmin
3 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 500 total points
Comment Utility
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.

Jawa29


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

'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
Next

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

Open in new window

0
 

Author Comment

by:BRPsysadmin
Comment Utility
I will test and get back to you
Thanks in advance
0
 
LVL 12

Expert Comment

by:prashanthd
Comment Utility
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




0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the frequent problems with the installations is when some file or registry entry is not removed from the system upon un-installation of the product. Clean removal is always highly desirable. One major reason for that is badly authored inst…
One of the most frequently asked questions on EE in the "Windows Installer" zone is how to eliminate self-triggered installation of some product.  The problem occurs when, suddenly, whenever a certain application is launched, or even when a folder i…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now