I think there is issue with
filename = filename.substring(filename.lastIndexOf("//"))
If you are using vbscript, then above statement is not correct.
One approach can be:
filename = split(filename,"/")
In above example, you need to check for empty string and uBound as well.