Batch file to test if scheduled task last result= 0x0

Posted on 2009-04-16
Last Modified: 2012-05-06
Hi Experts,

Can anyone give me a batch file  if statement to check last result of a scheduled task.

We use a scheduled task batch file that renames and moves backup files but it copies over good backups with bad ones.

I'd like to test if last resullt = 0x0 prior to the copy to ensure I don't loose good backups.



Question by:closehauled
    LVL 67

    Expert Comment

    You can use

    for /F "tokens=2 delims=:" %%R in ('schtask /query /TN "Taskname" /v ^| findstr /i result') do set /A result=%%R

    with "Taskname" replaced by the task you want to check.
    LVL 67

    Expert Comment

    for /F "tokens=2 delims=:" %%R in ('schtask /query /v /FO list /TN "Taskname" /v ^| findstr /i result') do set /A result=%%R

    Open in new window


    Author Comment

    Thanks for getting back to me on this.

    The /tn "taskname" parameter seems not valid for /query also had to use schtasks.

    LVL 67

    Accepted Solution

    Sorry for schtask(s).
    /query /TN  is only supported starting with Vista - didn't know that. That's making things a bit more complicated. We have to use CSV format. and see for the appropriate column, and filter for the task name.

    @echo off
    set result=
    for /F "delims=, tokens=7" %%R in ('schtasks /query /fo csv /v ^| findstr /L /C:"My Task"') do set result=%%R

    Open in new window


    Author Closing Comment

    Thanks, it works a charm.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now