Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help tweaking a script to back up Outlook PST file

Status
Not open for further replies.

Bruce33

IS-IT--Management
Dec 4, 2013
4
US
Hi everyone, I'm new to this forum and new to VBS script writing.
I searched through the threads on here regarding how to create a script to backup files and some great examples and tips and have attempted to write a script but do need some help tweaking it to make it fully functional.

What I am trying to do is:
NEED HELP - 1. Check to see if Outlook (version 2010) is open. If so then close outlook.
NEED HELP - 2. Back up the Outlook "archive.pst" file from a laptop C:\Users\current username\mail\archive.pst drive to a Microsoft server mapped drive Q:\current usernameCOMPLETED - 3. Give a prompt to the check if they want to backup the file.
COMPLETED - 4. Check if the archive file exists and if it does then prompt to overwrite.
COMPLETED - 5. Show progress of file being backed up and pop up a message window when completed.

I have completed #3-5 thanks to the tips I read on this forum. I'm able to back up the archive.pst file but I have to use my exact user name "bruce" in the filepath in the script instead of it automatically seeing who is currently logged in. So I need help to figure out how to:

A. On #1 above Close outlook if it is open. Possibly prompting the user to close it before continuing?
B. On #2 above Check and use the current username in the C:path and Q: path instead of using my specific "Bruce" name in the file path. That way anyone can use this script without having to modify it.

Here's my script so far:

' VBScript "Outback version 1" Project to copy the local outlook archive.pst file from the laptop C:\current user\mail to the mapped volume on the microsoft server Q:\current user' Written by Bruce33 on 2013-11-27
' &VbCr _ provides the carriage return and word wrap. Also I put 7 spaces on first message and 11 spaces on second message for centering

srcfile="C:\Users\Bruce\mail\archive.pst"
destdir="Q:\bruce\"
destfile=destdir & "\archive.pst"
Set Sh=WScript.CreateObject("WScript.Shell")
msg="Do you want to backup your Outlook archive file now?" & VbCr _
& VbCr _
& "Depending on file size this process can take up to 10 minutes to backup."

If Sh.Popup(msg,0,srcfile,33)<2 Then
Set fso=CreateObject("Scripting.FileSystemObject")
If fso.FileExists(destfile) Then
msg="Backup file already exists. Do you want to overwrite the file?"
If Sh.Popup(msg,0,destfile,33)>1 Then
WScript.Quit
End If
End If
Set SA=CreateObject("Shell.Application")
Set NS=SA.NameSpace(destdir)
NS.CopyHere srcfile,16
WScript.Echo "Backup completed! Have a Fantastic day!" & VbCr _
& VbCr _
& " Outback - v1 - written by Bruce33."
End If


 
Hello Bruce33

Don't forget to use code tags when posting your code... Here is some small pieces of scripts that will do what you are asking.

Code:
'Get Network Username
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
WScript.Echo objNetwork.UserName

This piece will close outlook, but will first check if it is running.
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")
For Each objProcess in colProcessList
   Set objOutlook = CreateObject("Outlook.Application")
   objOutlook.Quit
Next
 
To add to Nu2Java reply. You can check if the version is 2010 by simply looking at the .commandline of the process before closing it. Outlook 2010 version is from Office 14.

Code:
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'Outlook.exe'")
for each objProcess in colProcessList
   [red]if (inStr(lcase(objProcess.commandline), "outlook14")) then[/red]
      set objOutlook = CreateObject("Outlook.Application")
      objOutlook.Quit
   [red]end if[/red]
next

-Geates

NOTE: Not sure if the stand-alone version of Outlook installs to the office14 path. You'll have to verify.


 
Thank you both very much! and sorry about the delayed reply back as I've been on vacation.
The close Outlook coding worked perfectly thanks!

For the setting username correctly for file diestinations I'm not totally clear on how I should code this. Sorry for being slow here but keep in mind I'm a complete newbie to coding so I'll try to be clear in my following questions and break them in seperate coding sections.

My existing code to set where the LOCAL laptop hard drive outlook archive is located is
Code:
srcfile="C:\Users\Bruce\mail\archive.pst"

So how should the code be written to replace my existing code so that it detects the logged in user instead of me having to manually enter the word "Bruce" ?
Below is what I'm talking about but I'm unclear on how to code it correctly using your example?
Code:
srcfile="C:\Users\CURRENT LOGGED IN USER\mail\archive.pst"
What should the rewritten code line look like in order for it to work?

Also, for the NETWORK destination folder my existing code is below
Code:
destdir="\\SERVER123\q_archive\bruce\"

So how should the code be written to replace my existing code so that it detects the logged in user and sends it to the network destination instead of me having to manually enter the word "Bruce" ?
Below is what I'm talking about but I'm unclear on how to code it correctly using your example?
Code:
destdir="\\SERVER123\q_archive\CURRENT LOGGED IN USER\"
What should the rewritten code line look like in order for it to work?

Thanks again for your help :)

 
Not tested, but I think this will work:

Code:
Set objWSHshell = WScript.CreateObject("WScript.Shell")
strUser = objWSHshell.ExpandEnvironmentStrings("%USERNAME%")
strUserProfile = objWSHshell.ExpandEnvironmentStrings("%USERPROFILE%")

srcfile = strUserProfile & "\mail\archive.pst" 
destdir = "\\SERVER123\q_archive\" & strUser & "\"
 
Thanks again for everyone's help on this.

One more question. Currently in my script I have it deleting the .pst files in a folder but if there are no .pst files in the folder the program errors and stops. What is the command that I need to add to the below code to delete the .pst files and not error and quit if there are no .pst files?

my current code is below:
Code:
' this section deletes the *.pst files on the server Q drive in the mail folder (but doesn't delete anything in the subfolders) so that the user doesn't get prompted to overwrite the *.pst files
Const DeleteReadOnly = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("\\servername goes here\q_archive\username goes here\mail\*.pst"), DeleteReadOnly
 
One way is to tell the program to continue after an error (on error resume next), but then I would immediately undo that with "on error goto 0":

Code:
On Error Resume Next
objFSO.DeleteFile("\\servername goes here\q_archive\username goes here\mail\*.pst"), DeleteReadOnly
On Error Goto 0
 
Thankyou guitarzan! That worked perfectly! :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top