PROBLEM
Many sites are trying to explain it, but error message is pretty unclear. In my case I had a lot of pdf files to merge and for some reason one file was a big problem.
Ghostscript unrecoverable error undefinedfilename
SOLUTION
If you got this message then merge files with asterix
gs -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dQUIET -dSAFER -sOutputFile=out.pdf *.pdf
If out.pdf is OK It means "your file path or file does not exist". Now is the message "undefinedfilename" already clear.