Export Script with $if statement

I store "listened to" information in the track field %track%=zzzz or qqqq means I have listened to the podcast or it's in the queue. (this is due to a limitation of Copernic which I use to index my collection)

I want to identify all finished albums, i.e., albums where the podcasts all have zzzz or qqqq.

I set up the following export script:

$filename(Finished-Albums.m3u8,utf-8)
$loop(%album%)$loop(%album%)
$if(%track%,$puts(counttracks,$add($get(counttracks),1)),)
$loopend()
$if($eql(%_max_counter%,$get(counttracks)),'#Album Finished: '$if2(%album%,-- empty --)
,)
$puts(counttracks,0)
$loopend()

Investigation shows that the $if condition is being ignored ... that my user-defined field counttracks is being incremented to the total number of albums regardless whether %track% is present or not.

Thoughts?

Adapt the right coding for $put from there ...
[X] Export scripting: unexpected variable assignment between loops

Export script example 1:

1: $filename($getEnv('USERPROFILE')'\Desktop\Finished-Albums.txt',UTF-8)

2: $loop(%ALBUM%)$loop(%_path%)$puts(TrackCount,$if(%TRACK%,$add(1,$get(TrackCount)),$get(TrackCount)))$loopend()
3: $if($eql(%_max_counter%,$get(TrackCount)),' Album finished: ','# Album not finished: ')$if2(%ALBUM%,'[no album name]')$puts(TrackCount,0)$loopend()

Export script example 2:

01: $filename($getEnv('USERPROFILE')'\Desktop\Finished-Albums.txt',UTF-8)$puts(

02: )$loop(1,1)$puts(crlf,$char(13)$char(10))$puts(br,'{br}')$loopend()$replace(
03:
04: $loop(%ALBUM%)$replace(
05:
06: 'ALBUM: '$put(AlbumCount,$add(1,$get(AlbumCount)))' : '%ALBUM%$get(br)
07:
08: $loop(%_path%)$replace(
09:
10: 'File : '$put(FileCount,$add(1,$get(FileCount)))' : '%_path%$get(br)
11:
12: $puts(TrackCount,$if(%TRACK%,$add(1,$get(TrackCount)),$get(TrackCount)))
13:
14: ,$get(crlf),,$get(br),$get(crlf))$loopend()
15:
16: 'FileCount : '$get(FileCount)$get(br)
17: 'TrackCount: '$get(TrackCount)$get(br)
18:
19: $if($eql($get(FileCount),$get(TrackCount)),'# Album finished: ','### Album not finished: ')$if2(%ALBUM%,'[no album name]')
20: $get(br)$get(br)
21:
22: $puts(FileCount,0)
23: $puts(TrackCount,0)
24:
25: ,$get(crlf),,$get(br),$get(crlf))$loopend()
26: ,$get(crlf),,$get(br),$get(crlf))

DD.20140808.1044.CEST

Works great. Thanks. I never would have figured that out.
Great product!