如果其路径/名称包含空格,则系统找不到文件

时间:2019-04-06 06:03:15

标签: vbscript windows-10

Path = split(wscript.scriptFullName, wscript.scriptname)(0) 
CreateObject("wscript.shell").run(Path & "Name.txt")

如果文件路径和文件名均不包含空格,则上述脚本可以正常工作。

如果其中任何一个包含空格,则结果为;

  

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

如何解决该错误?

2 个答案:

答案 0 :(得分:2)

规则非常简单:

  1. 所有字符串都必须以双引号开头和结尾才能成为有效字符串。

    Dim a
    a = "Hello World" 'Valid string.
    a = "Hello World  'Not valid and will produce an error.
    
  2. 任何对变量的使用都必须使用String Concatenation字符&来将它们与字符串结合起来。

    Dim a: a = "Hello"
    Dim b
    b = a & " World" 'Valid concatenated string.
    b = a " World"   'Not valid and will produce an error.
    
  3. 由于使用双引号定义了字符串,因此必须通过将双引号""加倍来转义字符串中所有双引号的实例,但规则1仍然适用。

    Dim a: a = "Hello"
    Dim b
    b = """" & a & " World""" 'Valid escaped string.
    b = """ & a & " World"""  'Not valid, start of string is not complete 
                              'after escaping the double quote 
                              'producing an error.
    

遵循这三个规则,您不会出错。

请牢记以上几点;

CreateObject("wscript.shell").run("""" & Path & "Name.txt""") 

生成由文字双引号引起来的字符串。


有用链接

答案 1 :(得分:-3)

CreateObject("wscript.shell").run(""""Path & "Name.txt""")

是这样。