Vbscript Wscript.Run方法由于空格而被卡住 [英] Vbscript Wscript.Run method stuck because of space character
问题描述
错误系统找不到指定的文件
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
以上代码产生错误,因为它无法正确读取文件名。这都是因为文件名中的空格。阅读后,我发现chr(32)将替换空格,但不会。我如何使它占据空间字符。
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.
编辑:
我的最终代码看起来像这样有效。创建对象时我犯了错误。
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
推荐答案
shell将一个命令行分割为使用空白分隔符的参数。如果要将文本文件规范发送到.Run以在默认编辑器中自动显示,则必须双引号(逻辑)单个参数。这个演示代码:
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
将输出:
"C:\Documents and Settings\eh\tmp.txt" => -2147024894
""C:\Documents and Settings\eh\tmp.txt"" => 0
,只能打开一个记事本。
请参阅这里一些上下文。
这篇关于Vbscript Wscript.Run方法由于空格而被卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!