简体   繁体   English

如果双击但未在Windows计划任务中运行,则批处理文件正常运行

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

I have an archive.pst file on my C: drive that I use in outlook to backup my email. 我在我的C:驱动器上有一个archive.pst文件,我在outlook中使用它来备份我的电子邮件。 But my C: is not backed up each night. 但我的C:每晚都没有备份。 So I'd like to copy the .pst file to my network drive so it will consistently be backed up. 所以我想将.pst文件复制到我的网络驱动器,以便一直备份。 I do not want outlook to open the .pst file directly from the network drive for a variety of reasons. 我不希望outlook出于各种原因直接从网络驱动器打开.pst文件。

Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. 因此,我正在尝试创建一个计划任务,每天将我的.pst文件复制到网络位置。 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. Outlook不会关闭,并且不会复制.pst文件。 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. 1.使用完整路径指定作为计划任务执行的批处理文件中的所有文件。

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. 但是,当批处理文件作为计划任务运行时,Windows的system32目录是当前工作目录。

Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows? 在Windows的system32目录中是close_outlook.vbsopen_outlook.vbs吗?

I don't think so. 我不这么认为。 Replace in batch code below Path to\\Script File twice by the right path. 通过正确的路径将路径下的批处理代码替换Path to\\Script File两次。

2. Assign string values with space to environment variables right. 2.将带空格的字符串值分配给环境变量。

variable=value is the parameter for command set . variable=value是命令的参数。 With

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

you assign to variable idrive the value "\\\\myserver\\drive\\\\Outlook Files\\" with the double quotes included. 您为变量 idrive分配值为"\\\\myserver\\drive\\\\Outlook Files\\"并包含双引号。 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. 我在drive和文件夹路径末尾的反斜杠后也删除了第二个反斜杠。

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" . 使用"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. 3.使用命令ping更好地定义等待循环。

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. 现在,等待时间恰好是3秒钟。

4. Do not insert a space left of redirect operators > or >> for stdout. 4.不要为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. 5.避免在批处理文件本身或系统帐户中定义的环境变量。

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. 但是,许多批处理文件在双击时工作正常但未按计划任务运行失败,因为批处理文件依赖于环境变量(如PATH)或其他与当前用户帐户相关的变量。 It is safe to use environment variables which exist for all accounts like SystemRoot . 使用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. 这是您的批处理文件,其中包含相应的更改,在批处理文件(希望)作为计划任务运行之前,必须正确设置两个* .vbs文件的路径。

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

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. 我也尝试过将.vbs脚本移动到网络驱动器的相同任务。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM