Programmatically Check Dynamic File Name For Invalid Characters
Just run it through a series of REPLACE statements to get rid of any invalid characters:

SET @filename = REPLACE(@filename, '\','');
SET @filename = REPLACE(@filename, '/','');
SET @filename = REPLACE(@filename, ':','');
SET @filename = REPLACE(@filename, '"','');
SET @filename = REPLACE(@filename, '*','');
SET @filename = REPLACE(@filename, '?','');
SET @filename = REPLACE(@filename, '&lt;','');
SET @filename = REPLACE(@filename, '&gt;','');
SET @filename = REPLACE(@filename, '|','');


If it doesn't contain the character, it'll leave it unchanged. If it finds it, it'll remove it.