1

Here is the code. As I am new to batch script I cannot understand why .lock is used and why it is less than equal to 9.

set "lock=%temp%\wait%random%.lock"

start "" cmd /c 9>="%lock%1" abcd.bat 4441 %tempdate%

start "" cmd /c 9>="%lock%2" pqrs.bat 4442 %tempdate%

for %%N in (1 2 3 4 5 6 7 8 9) do (

        9>="%lock%%%N" || goto :Wait

) 2>nul
Cœur
  • 37,241
  • 25
  • 195
  • 267
Indranil
  • 2,229
  • 2
  • 27
  • 40
  • 1
    *.lock* is a file extension, and it is never compared against 9. The comparisons are done with against the environment variable *%lock%*. – IInspectable Nov 14 '16 at 08:37
  • 2
    There isn't any comparision at all. `9>=` is syntactical equal to `9>` and this is a file redirection operation. – jeb Nov 14 '16 at 08:50
  • If the purpose of this code is wait until all started processes ends, then a much simpler solution is given at [this answer](http://stackoverflow.com/questions/33584587/how-to-wait-all-batch-files-to-finish-before-exiting/33586872#33586872) – Aacini Nov 14 '16 at 16:00

1 Answers1

4

9> isn't a compare expression, it's a redirection of the output stream 9.
The syntax 9>= is nonsense, as the = has no meaning here as it will be dropped.

Output stream 9 normally doesn't exist, the output will be empty files "wait1000.lock1" and "wait1000.lock2" (assuming %random% is 1000 in this case).

The FOR loop simply tests if it can write to the same file, this will be blocked until the batch files exits and the write lock will be released.
And while at least one file is locked the command 9>"%lock%%%N" fails and then the goto :wait will be executed.

Btw. The label :Wait is missing in your sample file,
it should be inserted just before the FOR-loop

jeb
  • 78,592
  • 17
  • 171
  • 225