运行良好,如果双击但在Windows计划任务不运行批处理文件 [英] Batch File running fine if double-clicked but does not run in Windows Scheduled Task

查看:2012
本文介绍了运行良好,如果双击但在Windows计划任务不运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的C一个archive.pst文件:驱动器,我在Outlook中使用备份我的电子邮件。但我的C:没有备份,每个夜晚。所以我想将.pst文件复制到我的网络驱动器,所以它会持续进行备份。我不希望Outlook直接从各种各样的原因,网络驱动器打开.pst文件。

所以我想创建计划任务,将我的.pst文件每天复制到网络位置。下面的批处理文件完美地工作,如果双击。如果我尝试运行计划任务,只创建日志文件。 Outlook不关闭和.pst文件不会被复制。我试着以最高权限运行,但似乎并没有帮助。任何想法将AP preciated。

 的Cscript.exe close_outlook.vbs
    ::这是我的VBS脚本
        ::将Outlook设置=的CreateObject(Outlook.Application)
        :: Outlook.Quit平本地主机> NUL设置的iDrive =\\\\ MYSERVER \\驱动\\\\ Outlook文件\\
设置电流=C:\\ MyFolder文件\\ myuser的\\文件\\ Outlook文件复制的回声开始时间:%时间%GT;> %的iDrive%\\ Log.txt文件
副本%电流%\\ archive.pst%的iDrive%\\ archive.pst / Y
复制的回声结束时间:%时间%GT;> %的iDrive%\\ Log.txt文件移动的iDrive%%\\ Log.txt文件%的iDrive%\\ BACKUPLOGS \\ Log.txt文件
仁%的iDrive%\\ BACKUPLOGS \\ Log.txt文件%日期:10.4〜% - %日期:〜4.2% - %日期:7,2〜%_log.txt的Cscript.exe open_outlook.vbs
    ::这是我的VBS脚本
        ::一套SHELL = CREATEOBJECT(wscript.shell)
        :: shell.runOUTLOOK.EXE
出口


解决方案

1。指定为完整路径计划任务执行的批处理文件中的所有文件。

这通常与当前工作目录是批处理文件的目录中运行批处理文件的批处理文件的结果双击。但是,运行批处理文件作为计划的任务时,Windows中的system32目录是当前工作目录。

close_outlook.vbs open_outlook.vbs 在Windows的System32目录?

我不这么认为。在批处理code替换下面路径\\脚本文件在正确的道路的两倍。

2。分配空间字符串值的环境变量对的。

变量=值是命令参数的设置即可。随着

 设置的iDrive =\\\\ MYSERVER \\驱动\\\\ Outlook文件\\

分配给变量 的iDrive \\\\ MYSERVER \\驱动\\\\ Outlook文件\\用双引号包括在内。这导致对扩展

 呼应复制的结束时间:%时间%GT;> %的iDrive%\\ Log.txt文件

在命令行

 呼应复制的结束时间:19时21分53秒>> 1>中\\\\ MYSERVER \\驱动\\\\ Outlook文件\\\\ Log.txt文件

这是不对的,是不是。

正确的是:

 组的iDrive = \\\\ MYSERVER \\驱动器\\ Outlook文件

驱动器,并在文件夹路径的结束反斜杠后取出也第二个反斜线。

随着环境变量现在包含与没有双引号空间(S)的路径,双引号必须加入其中使用级联与文件名的环境变量的值,见下批次code

还有一个原因,使用变量=值。在使用命令行A端不可见尾随空格设置在批处理文件也被追加到环境变量的值,如果不使用或使用错误的双引号。阅读关于字符串值分配正确环境变量的详细信息这个答案

3。使用ping命令更好地定义等待循环。

命令

 平本地主机> NUL

产生等待。但最好是使用像

 的%SystemRoot%\\ SYSTEM32 \\ ping.exe的-n 4 127.0.0.1> NUL

像现在等待与正好3秒钟共同确定。

4。不要为标准输出插入左重定向操作符的空格>或>>。

为什么这个不应该做的是由我解释这里的细节。

5。避免环境变量不批处理文件本身或系统帐户中定义。

您的批处理文件使用批处理文件本身只定义了环境变量。所以这个建议是不是在这里真正需要的。

然而,许多批处理文件工作的罚款双击但不上运行的计划任务失败,因为该批处理文件依赖于环境变量,如 PATH 或他人这些都与当前的用户帐户。它是安全的使用的存在例如 SYSTEMROOT

所有帐户的环境变量

重做批次code

下面是相应的改变,因而两个* .vbs文件的路径必须由您的批处理文件之前设置正确的批处理文件(希望)可以作为计划任务。

 的%SystemRoot%\\ SYSTEM32 \\ Cscript.exe将路径到\\脚本文件\\ close_outlook.vbs
%SYSTEMROOT%\\ SYSTEM32 \\ ping.exe的-n 4 127.0.0.1> NUL设置的iDrive = \\\\ MYSERVER \\驱动器\\ Outlook文件
设置当前= C:\\ MyFolder文件\\ myuser的\\文件\\ Outlook文件复制的回声开始时间:%时间%GT;>中的iDrive%%\\ Log.txt文件
复制/ B / Y / Z当前%%\\ archive.pst%的iDrive%\\ archive.pst
复制的回声结束时间:%时间%GT;>中的iDrive%%\\ Log.txt文件将%的iDrive%\\ Log.txt文件%的iDrive%\\ BACKUPLOGS \\ Log.txt文件
仁的iDrive%%\\ BACKUPLOGS \\ Log.txt文件%DATE:10.4〜% - %日期:〜4.2% - %日期:7,2〜%_log.txt%SYSTEMROOT%\\ SYSTEM32 \\ Cscript.exe将路径到\\脚本文件\\ open_outlook.vbs设置的iDrive =
设置电流=

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

这篇关于运行良好,如果双击但在Windows计划任务不运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆