0

I have an archive.pst file on my C: drive that I use in outlook to backup my email. But my C: is not backed up each night. So I'd like to copy the .pst file to my network drive so it will consistently be backed up. I do not want outlook to open the .pst file directly from the network drive for a variety of reasons.

Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. The batch file below works perfectly if double-clicked. If I try to run the scheduled task, only the log file is created. Outlook doesn't close and the .pst file is not copied. I've tried running with the highest privileges but that doesn't seem to help. Any ideas would be appreciated.

cscript.exe close_outlook.vbs
    ::This is my VBS Script
        ::Set Outlook = CreateObject("Outlook.Application")
        ::Outlook.Quit

ping localhost > nul

set idrive="\\myserver\drive\\Outlook Files\"
set current="C:\myfolder\myuser\Documents\Outlook Files"

echo Start Time of Copy: %time% >> %idrive%\Log.txt
copy %current%\archive.pst %idrive%\archive.pst /y
echo End Time of Copy: %time% >> %idrive%\Log.txt

move %idrive%\Log.txt %idrive%\BackupLogs\Log.txt
ren %idrive%\BackupLogs\Log.txt %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt

cscript.exe open_outlook.vbs
    ::This is my VBS Script
        ::set shell = createobject("wscript.shell") 
        ::shell.run "outlook.exe"
EXIT
Eugene Astafiev
  • 26,795
  • 2
  • 13
  • 31
Sean B
  • 23
  • 2
  • Does the scheduled task run under your user account? – 404 Feb 20 '15 at 22:08
  • I'd recommend keeping files on the non-system (not C) drive. The C: drive requires admin privileges. – Eugene Astafiev Feb 21 '15 at 13:04
  • Do you have checked that the scheduled task runs under your user account? Why don't you simply close Outlook before you leave the computer for a longer time? – Mofi Feb 27 '15 at 06:36
  • Yes. The task is running under my user account. I _could_ close outlook before the task runs. I could also just manually run the batch file each morning when I go get coffee (my current method). But I was just trying to figure out how to make this work with a scheduled task. :-) – Sean B Mar 04 '15 at 13:55
  • Well, it is nearly impossible for us to find out what goes wrong when batch file is executed as scheduled task on your computer. I suggest to append to each line of the batch file `>>C:\Temp\Output.txt 2>>C:\Temp\Errors.txt` and make sure a directory `C:\Temp` exists with write permissions for everybody. Perhaps you can see on the 2 files what does not work when batch file is executed as scheduled task. – Mofi Mar 05 '15 at 07:11

2 Answers2

0

1. Specify all files in a batch file executed as scheduled task with full path.

Double clicking on a batch file results usually in running the batch file with currently working directory being the directory of the batch file. But when running a batch file as scheduled task, the system32 directory of Windows is the current working directory.

Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows?

I don't think so. Replace in batch code below Path to\Script File twice by the right path.

2. Assign string values with space to environment variables right.

variable=value is the parameter for command set. With

set idrive="\\myserver\drive\\Outlook Files\"

you assign to variable idrive the value "\\myserver\drive\\Outlook Files\" with the double quotes included. This results on expansion of

echo End Time of Copy: %time% >> %idrive%\Log.txt

in the command line

echo End Time of Copy: 19:21:53 >> 1>"\\myserver\drive\\Outlook Files\"\Log.txt

and this is not right, isn't it.

Correct is:

set "idrive=\\myserver\drive\Outlook Files"

I removed also second backslash after drive and the backslash at end of folder path.

As the environment variable contains now the path with space(s) without double quotes, the double quotes must be added where the value of the environment variable is used concatenated with a file name, see batch code below.

There is one more reason why using "variable=value". A not visible trailing space at end of the line with command set in batch file is also appended to value of the environment variable if double quotes are not used or used wrong. Read this answer for details about correct assignment of string values to environment variables.

3. Define the wait loop using command ping better.

The command

ping localhost > nul

produces a wait. But it is better to use something like

%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul

as now the wait is determined with exactly 3 seconds.

4. Do not insert a space left of redirect operators > or >> for stdout.

Why this should not be done was explained by me here in detail.

5. Avoid environment variables not defined in batch file itself or in system account.

Your batch file uses only environment variables defined in batch file itself. So this advice is here not really needed.

However, many batch file working fine on double click but not on running as scheduled task fail because the batch file depends on environment variables like PATH or others which are related to current user account. It is safe to use environment variables which exist for all accounts like SystemRoot.

Reworked batch code

Here is your batch file with the appropriate changes whereby the paths of the two *.vbs files must be set correct by you before the batch file (hopefully) works as scheduled task.

%SystemRoot%\System32\cscript.exe "Path to\Script File\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul

set "idrive=\\myserver\drive\Outlook Files"
set "current=C:\myfolder\myuser\Documents\Outlook Files"

echo Start Time of Copy: %time%>>"%idrive%\Log.txt"
copy /B /Y /Z "%current%\archive.pst" "%idrive%\archive.pst"
echo End Time of Copy: %time%>>"%idrive%\Log.txt"

move "%idrive%\Log.txt" "%idrive%\BackupLogs\Log.txt"
ren "%idrive%\BackupLogs\Log.txt" %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt

%SystemRoot%\System32\cscript.exe "Path to\Script File\open_outlook.vbs"

set "idrive="
set "current="
Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • Wow! Thank you so much for the detailed reply. I apologize for the delay in response back. This is my first post and although I checked the box to be emailed upon replies, I did not receive an email. I happened to check today and saw this reply in my inbox. Again, thank you. Unfortunately, after altering the code to yours, I'm still having the same problem. Outlook is not closing when I run the batch file as a scheduled task. The new batch also works when double-clicked though. – Sean B Feb 26 '15 at 16:45
0

In reviewing the previous responses, I have shortened the batch file to only the code below. This works when double-clicking, but not when scheduling a task. I've also tried the same task moving the .vbs script to a network drive. Same outcome.

%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\open_outlook.vbs"
Sean B
  • 23
  • 2