Vbscript Wscript.Run method stuck because of space character

荒凉一梦 提交于 2019-12-18 09:47: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

回答1:


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.



来源:https://stackoverflow.com/questions/17283956/vbscript-wscript-run-method-stuck-because-of-space-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!