Sharing improvements

- ```$fileList``` needs to be declared before the if-statement
- always check for a unique filename, fix #8673 

cc @PVince81