Move file existence test into loop condition