【问题标题】:Vbscript Wscript.Run method stuck because of space character由于空格字符,Vbscript Wscript.Run 方法卡住了
【发布时间】:2013-06-24 19:55:33
【问题描述】:

错误系统找不到指定的文件

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

上面的代码会产生错误,因为它无法正确读取文件名。这都是因为文件名中的空格字符。阅读后,我发现 chr(32) 会替换空格字符,但不会。我如何让它占用空间字符。

编辑:
我的最终代码看起来像这样有效。我在创建对象时犯了错误。

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

【问题讨论】:

  • 您必须双引号包含空格的文件规范。发布 strCline 的(典型)内容。
  • 我没听懂你。您是建议 1. "strLine" 2. strCline = replace(strCline, " "," ") 还是另一个? strCline 可能具有以下值:“第一个文本”或“第二个文本文件”。我不明白为什么在 oShell.Run get 执行时空格会被丢弃。

标签: dom vbscript hta


【解决方案1】:

shell 使用空格作为分隔符将命令行拆分为参数。如果要将文本文件规范发送到 .Run 以在默认编辑器中自动显示它们,则必须双引号(逻辑上)单个参数。本演示代码:

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

将输出:

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

只打开一个记事本。

有关上下文,请参阅here

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 2018-05-14
    • 1970-01-01
    • 2015-09-08
    • 2023-02-20
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多