Vbscript Wscript.Run method stuck because of space

2019-01-29 07:59发布

Error System can't find the file specified

strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing

Above code produces error because it can't read file name properly. It's all because of space characters in file name. After reading, i found chr(32) would replace space character but it won't. How do I make it take space character.

Edit:
My final code looked like this which worked. I made mistake while creating object.

Sub funEdit
set oShell=createobject("Wscript.shell")
strCline = Document.getElementById("head").innerHtml
msgbox strCline
strCline = replace(strCline, " ",Chr(32))
oShell.run strCline
Set oShell = Nothing
End Sub

标签: dom vbscript hta
1条回答
干净又极端
2楼-- · 2019-01-29 08:54

The shell splits a command line into parameters using blank(s) for a delimiter. If you want to send text file specifications to .Run to display them automagically in the default editor, you must double quote the (logically) single parameter. This demo code:

Option Explicit

Dim sFSpec : sFSpec = "C:\Documents and Settings\eh\tmp.txt"
Dim sCmd   : sCmd     = sFSpec
Dim oWSH   : Set oWSH = CreateObject("WScript.Shell")
On Error Resume Next
 oWSH.Run sCmd
 WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
 Err.Clear
 sCmd = qq(sFSpec)
 oWSH.Run sCmd
 WScript.Echo qq(sCmd), "=>", Err.Number, Err.Description
On Error GoTo 0

Function qq(s)
  qq = """" & s & """"
End Function

will output:

"C:\Documents and Settings\eh\tmp.txt" => -2147024894
""C:\Documents and Settings\eh\tmp.txt"" => 0

and open only one Notepad.

See here for some context.

查看更多
登录 后发表回答