VBScript中的文件名字符串空间问题 [英] Filename string space issue in VBScript

查看:36
本文介绍了VBScript中的文件名字符串空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此命令时出现错误,我不确定为什么.

运行VBScript以执行bat文件,我想将任何错误消息输出到日志文件.为此,我有以下代码:

 设置WshShell = CreateObject("WScript.Shell")WshShell.Run"cmd/k""C:\ ProgramTHISSTRING文件(x86)\ Folder \ File.bat">""C:\ Program Files(x86)\ Folder \ File.txt",1,真 

(我确实有代码可以更优雅地完成此操作,但出于问题的目的,我认为这样做更好,更快)

我也故意将THISSTRING放在下面的错误中.

执行此命令后,我会在CMD中收到此错误

'C:\ ProgramTHISSTRING'不被识别为内部或外部命令,可操作的程序或批处理文件.

我知道是文件名中的空格导致了错误,但是根据这个stackoverflow问题,为什么会发生此错误?

解决方案

我认为这里的问题是因为 cmd/k 需要将传递给它的命令封装在双引号中.

所以命令(删除了您刚才所说的 THISSTRING 只是为了触发错误)

  WshShell.Run"cmd/k""C:\ Program Files(x86)\ Folder \ File.bat">""C:\ Program Files(x86)\ Folder \ File.txt"",1,正确 

成为

  WshShell.Run"cmd/k"""C:\ Program Files(x86)\ Folder \ File.bat">""C:\ Program Files(x86)\ Folder \ File.txt"",1,真 

运行为

  cmd/k" C:\ Program Files(x86)\ Folder \ File.bat>" C:\ Program Files(x86)\ Folder \File.txt" 

I get an error when I run this command and I am unsure why.

Running VBScript to execute a bat file, I want to output any error messages to a log file. So to do this I have the code:

Set WshShell =  CreateObject("WScript.Shell")

WshShell.Run "cmd /k ""C:\ProgramTHISSTRING Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""", 1, True

(I do have the code to do this more elegantly but for the purpose of the problem, I think this reads better and quicker)

Also I have put in THISSTRING on purpose for the error below.

Once this executes I get this error in CMD

'C:\ProgramTHISSTRING' is not recognized as an internal or external command, operable program or batch file.

I understand it's a space in the file name that has caused the error, however, I have the right quotes according to this stackoverflow question so why is this error happening?

解决方案

I think the issue here is because the cmd /k needs the commands passed to it to be encapsulated in double quotes.

So the command (removed THISSTRING as you said that was just to trigger the error)

WshShell.Run "cmd /k ""C:\Program Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""", 1, True

Becomes

WshShell.Run "cmd /k """"C:\Program Files (x86)\Folder\File.bat"" > ""C:\Program Files(x86)\Folder\File.txt""""", 1, True

and run's as

cmd /k ""C:\Program Files (x86)\Folder\File.bat" > "C:\Program Files(x86)\Folder\File.txt""

这篇关于VBScript中的文件名字符串空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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