B
B
Blackster2019-08-19 14:47:01
cmd/bat
Blackster, 2019-08-19 14:47:01

Why BAT script works correctly only after the second run?

Hello! I wrote a simple script that should run the desired exe file with a parameter in a loop, with a pause between each run.

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
SET PROGRAM_PATH=D:\Program Files (x86)\Notepad++\notepad++.exe
SET LAUNCH_TIMEOUT=3
set mList=1.txt 2.txt 3.txt

(for %%m in (%mList%) do (
  echo %%m launch
  "!PROGRAM_PATH!" %%m
  echo %%m launched
  TIMEOUT /T "!LAUNCH_TIMEOUT!"
))

pause

The problem is that the first time you run this script, only the 1st file is opened, and that's it, the script is waiting for something incomprehensible, it just hangs, the next line of code after launch is not processed echo %%m launched. If you close it and run it again, then all 3 files open. Subsequent launches also work correctly.
I put this script in Windows startup, and after rebooting the computer, it just does not work as expected. And when restarted, it works. With manual start, everything is exactly the same as with autoload. The problem is not only with the launch of notepad ++, the script behaves this way when starting any program.
Tell me, what is my mistake? Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2019-08-19
@Blackster

Two points:
1. Parentheses are special characters in cmd, so they must be escaped:
I have the x64 version of notepad++, so I didn't check the brackets myself.
2. When you run a third-party application from a batch file, as you do, cmd waits for this application to complete and only then continues the script execution further.
To remove this expectation, you need to start using the start command as follows: The first quotes are the title of the new window, in this case it does not matter, because notepad++ will set its title. In general, your script worked for me after I corrected the path to notepad++ and added start.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question