none
VBS problem with spaces using fso.OpenTextFile(str).ReadAll

    Question

  • I have an hta written in VBS that uses .OpenTextFile(strDirectory).ReadAll as an "include" statement to make functions in a vbs file locally accessible to the hta code. 
    There are 3 points to my problem that seem significant:
    1. On 'my system' (the LAN I work in and developed the code on) has no problems and runs fine under all circumstances

    2. On 'the other' system in my company (I don't really understand what differences there are between the systems, but I have to sign into a security portal to gain access to the other drives and/or virtual terminals that run on the other system) the .OpenTextFile(strDirectory).ReadAll statements fail with "path not found" if there are spaces in the path. It fails with 'bad file name or number' when I try to put quotes around it.

    3. I'm "almost" sure it's a 'spaces' issue because I can run it on the old system if I take the space out of the parent folder name and run as an hta.  However I want to make it an .exe which makes it run from a local temp file which will have spaces in it.

    Here's the relevant code:

    intpos=InStrRev(document.location.pathname,"\")
    strCurrentDirectory=""
       If intpos>0 Then
         strcurrentDirectory = Left(document.location.pathname,intpos)
         strCurrentDirectory = strCurrentDirectory
       End If


    strDirectory = strCurrentDirectory & "GetProjectFolders.vbs"
    strDirectory = Chr(34)& strDirectory & Chr(34)

    ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(strDirectory).ReadAll


    I've tried this with double quotes (Chr(34)) as well and it also gives me 'bad file name or number'. 

    I've searched hi and low on the web to find the proper way to quote out the spaces and just can't find a solution.  How are these paths supposed to be quoted for spaces?

    Wednesday, November 25, 2009 3:11 PM

Answers

  • Have you tried looking at the contents of the first instance of strCurrentDirectory as part of your troubleshooting?  If you had, you would have seen that the HTA had done its internet thing and 'escaped' the spaces into their hex equivalent ASCII code with a leading space - that is %20.  This is standard procedure for URLs, which cannot have spaces and certain other punctuation marks.  However, as you've found, the FSO does not like that.  The solution is to 'unescape' the string as in ...

         strcurrentDirectory = unescape(Left(document.location.pathname,intpos))

    Now it should work, as is, without any extra quotes.

    BTW, what is the next line of code supposed to do? 
    Tom Lavedas
    Wednesday, November 25, 2009 4:03 PM

All replies

  • Have you tried looking at the contents of the first instance of strCurrentDirectory as part of your troubleshooting?  If you had, you would have seen that the HTA had done its internet thing and 'escaped' the spaces into their hex equivalent ASCII code with a leading space - that is %20.  This is standard procedure for URLs, which cannot have spaces and certain other punctuation marks.  However, as you've found, the FSO does not like that.  The solution is to 'unescape' the string as in ...

         strcurrentDirectory = unescape(Left(document.location.pathname,intpos))

    Now it should work, as is, without any extra quotes.

    BTW, what is the next line of code supposed to do? 
    Tom Lavedas
    Wednesday, November 25, 2009 4:03 PM
  • It's not supposed to do anything.  It was left over from me goofing around trying to see what the problem was and missed it when posting this topic....

    I have looked at the contents, with msgbox, and it comes out with the %20.  I've tried to do a replace(strcurrentDirectory,"%20," ") but it had no affect.


    I am trying the unescape now and will report back.
    Wednesday, November 25, 2009 4:12 PM
  • Thanks Tom, that did it.  Plus now I understand where the %20 comes from.  I've wrestled with it a couple of times now and never really understood where it was coming from.  Thanks for your help.
    Wednesday, November 25, 2009 4:17 PM