Please post you code in "code" tags - it's a devil to read if you don't.

To fix your aborting problem when the share name contains special characters, change this line:
Code:
$rc=execute('$$resource='+$resource)



to this:
Code:
$rc=execute('$$resource="'+$resource+'"')



The problem is that the bare share name will be interpreted as KiXtart script code, so you need to protect it with speech marks.