user338034
user338034

Reputation: 443

Why does batch file execution stop before processing the second "call" statement?

I've a fairly rudimentary knowledge of batch files and usually manage to get by but have hit a problem which I can't solve.

The batch files are run on Windows 7 Ultimate and Windows 10 Professional systems and are usually invoked by a Scheduler program, although sometimes I just click the relevant desktop icon. Essentially, the role of the batch files is to download specific files (TV programmes) which are listed in an external text file, in my case, located in my Dropbox account. For each item in the text file (TV.txt) there are two lines, one naming the file, the other listing its ID:

name1
ID1
name2
ID2
name3
ID3

The batch files successively work through the items listed in the text file, one file works on the "IDs", the second on the "names".

The "IDs" file (tv-pid.cmd) consists of the following:

set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a

The "names" file (tv-nopid.cmd) consists of the following:

set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a

Each batch file works well on its own, the problem is when I try to combine the two into a single batch file.

If I create a "combined" batch file (tv.cmd):

call tv-pid.cmd
call tv-nopid.cmd

the first "call" is executed but the batch operation terminates before calling the second file.

Equally if I create a "combined" batch file (not using "call" commands)

set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a

the same happens, the download activity on line 2 is executed after which the batch operation terminates.

Personally I would prefer a solution based on the "call" commands, but I don't mind.

Upvotes: 0

Views: 222

Answers (1)

Magoo
Magoo

Reputation: 79983

set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
set "idnames="
for /f "delims=" %%a in ('type %$textFile%') do (
 if defined id_names (
  set "id_names="
  call get_iplayer --pid %%a
 ) else (
  set "id_names=Y"
  call get_iplayer --get %%a
 )

This may work. I've no idea what get_iplayer is or does.

The idea here is that the line-content alternates, so toggling the variable id_names between set-to-a-value and set-to-no-value (=cleared) allows us to execute get_iplayer with the correct options.

Note that your code would execute get_iplayer with the option pid or get for each line of the input file - which may be causing the problem.

Upvotes: 3

Related Questions