robocopy suspend

I made a simple batch file to copy outlook pst files to a network share. this is what i have so far

taskkill /T /F /IM OUTLOOK.EXE

robocopy "C:\outlook" "H:\Outlook Files" /fft /XO /E

I found that i need to pause this simple script between when it closes outlook, and starts copying, but I can't figure out how to create a pause. Any ideas how I could do this?
LVL 1
JeffBeallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Well, you can always do a few pings to localhost if you know the maximum delay required; for example 10 seconds:
ping.exe -n 11 localhost

Open in new window

What's the error you get when you start too early? By default, robocopy tries a million times with a 30 second pause between each attempt when it encounters an error (or did you save these values in the registry at some point?).
0
Joe Winograd, Fellow&MVEDeveloperCommented:
Hi Jeff,
Yes, it's a pity that batch files don't have a simple Sleep command, but there are numerous ways to achieve this. One is to use the Choice command with the timeout parameter. Here's what the command is for a 5-second delay (change the 5 to however many seconds you want to pause):

choice /d y /t 5

Regards, Joe
0
Bill PrewCommented:
A better approach would be to loop while outlook.exe is still running, and then continue when it ends.  But this question closed very quick so I'll assume that wasn't useful and move along to other open questions.

~bp
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Joe Winograd, Fellow&MVEDeveloperCommented:
Hi Bill,
According to my browser, this question is still open. Regards, Joe
0
Bill PrewCommented:
Wasn't a minute ago!

:)
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Why do you need to wait? RoboCopy with the default settings retries to copy locked files 1 million times with a 30 seconds pause. That should suffice.
0
Bill PrewCommented:
If you do want to kill outlook, and wait for it to end before starting, then here's an example of an approach for that.

@echo off
setlocal

REM Define task to kill
set KillTask=outlook.exe

REM Invote the kill of this task
taskkill /IM /T /F %KillTask% >NUL 2>&1

REM Loop until the task is no longer running before proceeding
:WaitLoop
  REM Delay 1 second (in milliseconds) between checks
  ping -n 1 -w 1000 192.0.0.0 >NUL 2>&1
  REM If task is still running, loop until it isn't
  tasklist /FI "imagename eq %KillTask%" | find /I "%KillTask%" >NUL 2>&1 && goto :WaitLoop

REM Do the copy now that our task has stopped
robocopy "C:\outlook" "H:\Outlook Files" /fft /XO /E

Open in new window

~bp
0
JeffBeallAuthor Commented:
ok, so this is what i found, first off, the ping pause and the choise pause both work. so thank you for that.
Why wait, I'm not exactly sure, all i know is that from trial and error, if i try

taskkill /f /t /im outlook.exe

it closes outlook, but for some reason, when i copy something to the pst, then the batch closed outlook with taskkill the things that were copied to the pst are not there when i open outlook again. So I close outlook by clicking on the red X in the upper right corner of the windows, then after the copy command runs and i open outlook again, the stuff in the pst is there
so i googled some stuff and found this

pushd %~dp0
cscript closeoutlook.vbs

I put my batch and this vbscript in the same directory and now the batch looks like this

pushd %~dp0
cscript closeoutlook.vbs

choice /d y /t 5

robocopy "C:\outlook" "H:\Outlook Files" /fft /XO /E

and on my computer this works great, stuff i copy to the pst, then run the batch, it closes outlook, pauses 5 seconds, then copies the pst to my "H" drive. then i open outlook and everything that i put in the pst is still there.
now the problem is i copied this whole setup on a user's pc, but

pushd %~dp0
cscript closeoutlook.vbs

isn't closing outlook on her computer.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Post the VBS script or the link to the source, please.
0
JeffBeallAuthor Commented:
Set objOutlook = CreateObject("Outlook.Application")
objOutlook.Quit

that is the script, but I'm sorry, I don't know where I got it. It was earlier in the day and I closed my browser. I have my browser set to always run in private mode. sorry.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That's all? Two reasons why that script might not work are apparent:
a) Outlook is not found, or the above starts a new Outlook, which is then closed instead.
b) Outlook does prevent closing, e.g. because it is blocked, doing something, or changes may got lost at that moment. E.g. Outlook will not close while writing a message; a save prompt is displayed. There might be something in the background (VBA project changes, ...).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JeffBeallAuthor Commented:
ok, I found this script, hopefully it's better.

On Error Resume Next
Set Outlook = GetObject(, "Outlook.Application")
If Err = 0 Then
Outlook.Quit()
Set Outlook = Nothing
End If
Wscript.Quit

I don't know how to script, but it seems like this has some logic to it. I guess it looks for outlook, and closes outlook if it finds it
0
Bill PrewCommented:
So, does that work now on both test PCs?

~bp
0
Bill PrewCommented:
Depending on how you make out, here are a couple of small utils you could also explore for safely closing outlook.

https://www.maxoutput.com/SendMsg.html

http://www.aswit.com/akill/

~bp
0
Joe Winograd, Fellow&MVEDeveloperCommented:
Jeff,
I run a script every day in the wee hours to close Outlook so the PST can be backed up. It has never failed to close Outlook in years of usage. I wrote it in AutoHotkey, but there are a variety of languages you could write it in. The basic technique is to send Alt-F4 to Outlook. If you'd like to try AutoHotkey, here's the code:
SetTitleMatchMode,2 ; set title matching behavior to CONTAINS
IfWinExist Outlook ; check if "Outlook" exists in any title bar
{
  WinActivate ; activate the Outlook window
  Send !{F4} ; send Alt-F4 to close Outlook
}

Open in new window

Put the above in a file called <ExitOutlook.ahk>. After installing AutoHotkey, it will own the file type AHK. Then put this call in Task Scheduler:

D:\scripts\exitoutlook.ahk

Of course, <scripts> is wherever your scripts are stored. If you'd prefer a stand-alone/no-install executable to run on a computer that doesn't have AutoHotkey installed, simply compile the AHK file (right-click/Compile) on any computer with AutoHotkey installed and put the EXE file that the compiler creates on any computer where you want it to run. Then your Task Scheduler call would be:

D:\whateverfolder\exitoutlook.exe

Regards, Joe
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The SendKey approach could be used in VBS too, but trying to get the Outlook object and sending a close command is much better, so I would stay with the new VBS script. For Outlook it should not matter whether you use GetObject or CreateObject, as you can't create more than one instance (AFAIK) of Outlook anyway - CreateObject just returns the pointer to the already running Outlook.
0
JeffBeallAuthor Commented:
ok, for whatever reason, on one of the user's computers. outlook refuses to close with
taskkill /IM /T /F OUTLOOK.exe >NUL 2>&1
or
On Error Resume Next
 Set Outlook = GetObject(, "Outlook.Application")
 If Err = 0 Then
 Outlook.Quit()
 Set Outlook = Nothing
 End If
 Wscript.Quit
so I started thinking, would it be possible to have a script that logs off the user - which would make windows kill the outlook process. and then copy the pst file to a network share? I'm assuming that since the script would be started by the user, windows would kill all of the user's processes, including the script. but is it possible to have a script run as a service maybe?
also, I'm wanting to save the pst file to a network share - but given the nature of a pst file, i assume the whole file has to be copied up each time. So if it is a 2gb file, every time it will copy 2gb, and not just changes to the pst.
lastly, i thought maybe cached mode would work, but cached mode in outlook, i thought only make a local copy of the mailbox, and ignores pst files
I also wanted to mention Joe Winograd, I'm not ignoring your suggestion - I can't install something like AutoHoyKey in my environment. only approved software is allowed. were I used to work, I used AutoIt, which sounds a lot like AutoHotKey.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You really should check why Outlook does not close. Try doing it manually - maybe it just last very long for Outlook to properly close?
0
JeffBeallAuthor Commented:
I found a script that works! ( it does close manually ), anyhow i found this

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

I don't know why it works, I just know it does!
0
JeffBeallAuthor Commented:
thank you for the help, I think I got points to everyone.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

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.