Share via

Problem with Coding a .bat file to run a TASKLIST command

Anonymous
2023-10-26T05:09:22+00:00

Problem with Coding a .bat file to run a TASKLIST command

I want to initiate a ROBOCOPY process if EXCEL is running in my System.

To do this, I have created the following .txt file –

 N.B. The ROBOCOPY process works exactly as I want – tested separately.

I then created a .bat file from this .txt file, and a Shortcut for the .bat file.

My Task Scheduler job then runs that Shortcut.

However, the TASKLIST process does not work – i.e.  whether or not EXCEL is running, the ROBOCOPY command is not executed.

The scheduled process keeps running until it is ended manually in Task Scheduler.

Please help with corrections for my coding of the TASKLIST process, or any other errors I have made.

Microsoft 365 and Office | Install, redeem, activate | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-10-26T09:28:28+00:00

Hello

I’m Adeyemi and I’d be happy to help you with your question.

I understand that you're trying to run a ROBOCOPY process if EXCEL is running on your system, and you're having trouble with the TASKLIST command in your .bat file. Here's a way to check if EXCEL is running using the TASKLIST command:

tasklist /FI "IMAGENAME eq EXCEL.EXE" 2>NUL | find /I /N "EXCEL.EXE">NUL
if "%ERRORLEVEL%"=="0" (
    REM Your robocopy command here
)

This script checks if any Excel file is open or running. If Excel is running, the error level will be 0, and the commands inside the parentheses will be executed. You can replace REM Your robocopy command here with your ROBOCOPY command.

If you're still having issues, it might be due to how you're calling the .bat file in Task Scheduler. When setting up the task in Task Scheduler, try configuring it to execute cmd.exe. In the arguments field, put /c path_to_your_bat_file > path_to_your_log_file 2>&1. This will execute your .bat file and output any errors to a log file, which can help with troubleshooting.

Please replace path_to_your_bat_file with the actual path to your .bat file, and path_to_your_log_file with the path where you want to save the log file. For example:

/c C:\YourFolderName\BackupProg.bat > C:\YourFolderName\BackupProg.log 2>&1

I hope this helps

Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below

Regards Adeyemi

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful