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

后端 未结 2 417
失恋的感觉
失恋的感觉 2021-01-29 02:15

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

相关标签:
2条回答
  • 2021-01-29 03:09

    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="
    
    0 讨论(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"
    
    0 讨论(0)
提交回复
热议问题