How to avoid error code 1 when running batch file in Task Scheduler?

CharlieLor 566 Reputation points
2023-06-07T16:38:25.8366667+00:00

This is the script I have running in my Windows Server Task Scheduler.

Forfiles.exe -p C:\temp\LogFiles\ -m *.log -d -10 -c "Cmd.exe /c del @path"

Task Scheduler reports error code 1 if there are no files that are older than 10 days old. How do I avoid getting this error code 1?

Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Khaled Elsayed Mohamed 1,335 Reputation points
    2023-06-11T11:59:17.2566667+00:00

    Hi CharlieLor

    You can avoid getting error code 1 by adding the IF statement to your script. The IF statement will check if there are any files that match the criteria and only execute the del command if there are.

    Here is the modified script:

    Forfiles.exe -p C:\temp\LogFiles\ -m *.log -d -10 -c "IF EXIST @path (Cmd.exe /c del @path)"

    The IF statement will check if the file exists at the specified path. If the file exists, the del command will be executed. If the file does not exist, the del command will not be executed.

    This will prevent Task Scheduler from reporting error code 1 if there are no files that match the criteria.

    Here is a more detailed explanation of the IF statement:

    • IF is a keyword that is used to check a condition.
    • EXIST is a function that checks if a file exists.
    • @path is a variable that contains the path to the file.
    • (Cmd.exe /c del @path) is a command that deletes the file.

    By combining these elements, the IF statement checks if the file exists at the specified path. If the file exists, the del command will be executed. If the file does not exist, the del command will not be executed.

    0 comments No comments

  2. Khaled Elsayed Mohamed 1,335 Reputation points
    2023-06-11T12:01:16.9966667+00:00

    Hi CharlieLor

    another solution:

    To avoid receiving an error code 1 when using the Forfiles command in your Task Scheduler script, you can add a conditional statement to handle the scenario where no files are found. Here's an example of how you can modify your script:

    @echo off
    
    forfiles /p "C:\temp\LogFiles\" /m *.log /d -10 /c "Cmd.exe /c del @path"
    
    REM Check the error level and handle it accordingly
    IF %errorlevel% NEQ 1 (
        echo Files older than 10 days deleted successfully.
    ) ELSE (
        echo No files found older than 10 days.
        REM Perform any necessary actions or skip further steps.
    )
    

    In this modified script, the IF %errorlevel% NEQ 1 statement checks if the error level returned by the forfiles command is not equal to 1. If the error level is not 1, it means that files were found and deleted successfully. In this case, the script echoes a success message.

    In this modified script, the IF %errorlevel% NEQ 1 statement checks if the error level returned by the forfiles command is not equal to 1. If the error level is not 1, it means that files were found and deleted successfully. In this case, the script echoes a success message.

    In this modified script, the IF %errorlevel% NEQ 1 statement checks if the error level returned by the forfiles command is not equal to 1. If the error level is not 1, it means that files were found and deleted successfully. In this case, the script echoes a success message.

    If the error level is equal to 1, it means that no files matching the criteria were found. The script echoes a message stating that no files were found and you can perform any necessary actions or skip further steps based on this condition.

    By including this conditional statement, you can handle the case where no files are older than 10 days without the script reporting an error code 1.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.