Easiest would be to keep generating temp file names with the .tmp extension to a string, stripping the .tmp and adding whatever extension you like, then checking to see if the new filename exists. Lather, rinse, repeat until you find a unique one.