VBS中解决路径带空格的三种方法

简介: vbs中,如果需要运行的程序中带有空格,按照通常的方式往往会提示错误,其实有两种形式不同的解决方法: 在应用程序前后分别加三个双引号,代码如下:     [c-sharp] view plaincopyprint? Set wshell=CreateObject("WScript.

vbs中,如果需要运行的程序中带有空格,按照通常的方式往往会提示错误,其实有两种形式不同的解决方法:

  1. 在应用程序前后分别加三个双引号,代码如下:

 

 

[c-sharp] view plain copy print ?
  1. Set wshell=CreateObject("WScript.Shell")  
  2. wshell.Run  """C:/Program Files/360/360se/360se.exe""",5,True   
  3. Set wshell = Nothing  

Set wshell=CreateObject("WScript.Shell") wshell.Run """C:/Program Files/360/360se/360se.exe""",5,True Set wshell = Nothing

 

 

  1. 使用chr(34)对字符串加引号,代码如下:

 

 

[c-sharp] view plain copy print ?
  1. temp="C:/Program Files/360/360se3/360se.exe"  
  2. path = Chr(34) & temp & Chr(34)  
  3. Set wshell=CreateObject("WScript.Shell")  
  4. wshell.Run path,1,True   
  5. Set wshell = Nothing  

temp="C:/Program Files/360/360se3/360se.exe" path = Chr(34) & temp & Chr(34) Set wshell=CreateObject("WScript.Shell") wshell.Run path,1,True Set wshell = Nothing

 

 

3. 为了增加可读性,使用一种定义常量的方式,代码如下:

 

[c-sharp] view plain copy print ?
  1. Public Const vbQuote = """"  
  2. temp="C:/Program Files/360/360se3/360se.exe"  
  3. path = vbQuote & temp & vbQuote  
  4. Set wshell=CreateObject("WScript.Shell")  
  5. wshell.Run path,1,True   
  6. Set wshell = Nothing  

Public Const vbQuote = """" temp="C:/Program Files/360/360se3/360se.exe" path = vbQuote & temp & vbQuote Set wshell=CreateObject("WScript.Shell") wshell.Run path,1,True Set wshell = Nothing

 

 

一些解释:

  1. 因为vbs将双引号视为一个值的容器,所以你如果需要使用双引号作为一个值使用,那么需要在前后使用一个双引号来说明。
  2. 而Chr(integer i)则是返回ascii码表中i对应的字符,34在ascii码表中对应双引号。

 

技术改变世界! --狂诗绝剑
目录
相关文章
|
Java Windows 应用服务中间件
|
Shell
SHELL脚本递归循环,文件名包含空格的处理办法
SHELL脚本递归循环,文件名包含空格的处理办法
164 0
|
Shell
SHELL判断文件是否包含某个字串
SHELL判断文件是否包含某个字串
156 0
|
Shell
用shell把所有文件名修改为小写
用shell把所有文件名修改为小写
109 0
dlopen失败一例:路径字串多一个回车,导致文件找不到
dlopen失败一例:路径字串多一个回车,导致文件找不到
93 0
|
Shell
SHELL中从变量中截取后缀、文件名、目录名
SHELL中从变量中截取后缀、文件名、目录名
136 0
shell遍历文件夹及去掉文件后缀名
shell遍历文件夹及去掉文件后缀名
cygwin中参数目录有空格怎么办?
cygwin中参数目录有空格怎么办?
127 0