It seems that your problem deals with having spaces in filenames.
The solution to this is simply an extra step which I have come to think of and use over the past some years.
Put simply, you could output your statements (literally: echo "echo \"$filename\"" >> temp_file) and have that output to a temporary file. This temporary file should look like:
echo "file with space.txt"
Now have this temporary file output and executed by bash:
cat temp_file | bash
This takes care of spaces, by treating each line as an entity rather than each word. This is yet another reason to not have spaces in filenames. I only use the aforementioned technique for my music, which must have spaces.