History: 2009-11-18: ConvertToURL was modified:
- To take into account % char
- To return the file name unchanged when it starts with "file://"

I call the following ConvertToURL function when I need to open/save files from the Open Office APIs.



Source:

Option Explicit ' Doing so is safest 
Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

If wscript.arguments.count <> 1 Then
  wscript.echo "Usage: " & vbCRLF & vbCRLF
  wscript.echo "CSCRIPT //NOLOGO ConvertToURL.vbs filename"
  Wscript.Quit(2) 'Let's leave returning an ERRORLEVEL 2
End If

wscript.echo ConvertToURL(wscript.arguments(0))

Wscript.Quit(1) 'My prefered way to handle unhandled errors

Function ConvertToURL(sFileName)
Dim sTmpFile

  If Left(sFileName, 7) = "file://" Then
    ConvertToURL = sFileName
    Exit Function
  End If

  ConvertToURL = "file:///"

  sTmpFile = fso.GetAbsolutePathName(sFileName)

  'replace any "\" by "/"
  sTmpFile = Replace(sTmpFile,"\","/") 

  ' replace any % by %25
  sTmpFile = Replace(sTmpFile,"%","%25") 

  'replace any " " by "%20"
  sTmpFile = Replace(sTmpFile," ","%20")

  ConvertToURL = ConvertToURL & sTmpFile
End Function 
 




Tests:


CommandLine cscript //NOLOGO ConvertToURL.vbs "c:test.txt"
Output file:///C:/Documents%20and%20Settings/nicetuna/test.txt


CommandLine cscript //NOLOGO ConvertToURL.vbs "test.txt"
Output file:///E:/project/DB2XLS/test.txt


CommandLine cscript //NOLOGO ConvertToURL.vbs "\\%computername%\e$\project\db2xls\é a\test.txt"
Output file://///XP/e$/project/DB2XLS/é%20a/test.txt