Batch File running fine if double-clicked but does not run in Windows Scheduled Task

自作多情 提交于 2019-12-20 07:26:49

问题


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

回答1:


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"



回答2:


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="


来源:https://stackoverflow.com/questions/28637873/batch-file-running-fine-if-double-clicked-but-does-not-run-in-windows-scheduled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!