You also got some copy paste errors in there. There's an ENDIF missing in the top portion of code, and a random ENDIF and NEXT at the end of the dirlist() function. Also missing the EndFunction at the end of dirlist().