Also, in the script you posted, there is a line that is missing a $.

FOR EACH $Z in FileList

should be...

FOR EACH $Z in $FileList