Solved

VBScript to enumerate folders and delete a file if it exists

Posted on 2011-03-09
3
779 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 500 total points
ID: 35084972
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
ID: 35084999
I will test and get back to you
Thanks in advance
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35085776
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 major drawbacks of deploying applications by GPO is the complete lack of any centralized reporting. After a normal deployment, there are two ways to find out if it was successful – by looking in the event log, and by looking in the log of…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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