Vbscript Wscript.Run 方法因空格字符卡住 [英] Vbscript Wscript.Run method stuck because of space character

查看:21
本文介绍了Vbscript Wscript.Run 方法因空格字符卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Settingseh	mp.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 Settingseh	mp.txt" => -2147024894
""C:Documents and Settingseh	mp.txt"" => 0

并且只打开一个记事本.

请参阅此处了解一些上下文.

See here for some context.

这篇关于Vbscript Wscript.Run 方法因空格字符卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆