Run programs with too many spaces

社会主义新天地 提交于 2019-11-27 07:37:27

问题


I have a command that works fine with command prompt:

CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep""

Here I need CMD /C to return the errorlevel.

That's how I tried to run this in VBScript using Run method:

WshShell.Run "CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep""", 0, False

This command throws the following error:

Expected end of statement (Char: 74)

I saw many similar questions about running programs with spaces in its and its parameters' paths. I tried changing above command into hundreds of different commands with adding more double quotation marks into different positions as I saw in answers, but nothing worked and everytime I ran the script, one of the following errors occured:

Expected end of statement
The system cannot find the file/path specified (null)
Unterminated string constant
Expected ")"
Expected "("


回答1:


Lankymart's answer is right:

You just need to escape the quotes in the string correctly, the rule is whenever you want to show a quote in a string double it.

However, you still have unbalanced double quotes in the original code (a surplus trailing ").

WshShell.Run "CMD /C """"C:\Program Files (x86)\VideoLAN\VLC\VLC"" -vvv ""http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO=="" :sout=#file{dst=""F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4"",no-overwrite} :sout-keep"""

This will run as

CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep"

Sample VBScript (a particular executable cliparserPause.exe is used to mimic VLC behaviour):

option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim sCmdToRun, WshShell, intReturn

' CMD /C ""D:\bat\Prog Files (x86)\cliparserPause.exe" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep"
sCmdToRun = "CMD /C """"D:\bat\Prog Files (x86)\cliparserPause.exe"" -vvv ""http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO=="" :sout=#file{dst=""F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4"",no-overwrite} :sout-keep"""
strResult = strResult & vbNewLine & sCmdToRun

Set WshShell = WScript.CreateObject("WScript.Shell")
intReturn = WshShell.Run( sCmdToRun, 1, true)
strResult = strResult & vbNewLine & Cstr( intReturn )

Wscript.Echo strResult
Wscript.Quit( intReturn )

Output:

==> CMD /C ""D:\bat\Prog Files (x86)\cliparserPause.exe" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C
 Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep"
param 0 = D:\bat\Prog Files (x86)\cliparserPause.exe
param 1 = -vvv
param 2 = http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==
param 3 = :sout=#file{dst=F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4,no-overwrite}
param 4 = :sout-keep
press any key to continue...

==> echo %errorlevel%
-1004

==> cscript //NOLOGO D:\VB_scripts\SO\43649265.vbs
43649265.vbs
CMD /C ""D:\bat\Prog Files (x86)\cliparserPause.exe" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Bac
kup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep"
-1004

==> echo %errorlevel%
-1004

cliparserPause.exe source:

#include "stdafx.h"
#include <wchar.h>
#include <cstdio>
#include <stdlib.h>

int main(int argc, wchar_t* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
        wprintf(L"param %d = %S\n", i, argv[i]);
    }
    wprintf(L"press any key to continue...");
    std::getchar();
    exit(-999 - argc);  /* exitcode to OS = ( -1000 -supplied_paramaters_count ) */
    return 0;
}



回答2:


If you followed this answer you would realise that none of the double quotes in your string are escaped.

From A: Not able to launch bat file form VBScript if path contains a space
You just need to escape the quotes in the string correctly, the rule is whenever you want to show a quote in a string double it.

So the line should be

WshShell.Run "CMD /C """"C:\Program Files (x86)\VideoLAN\VLC\VLC"" -vvv ""http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSig‌n=c2VydmVyX3RpbWU9NC‌8yNy8yMDE3IDEyO=="" :sout=#file{dst=""F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4"",no-overwrite} :sout-keep""""", 0, False

Which will run as

CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSig‌n=c2VydmVyX3RpbWU9NC‌​8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep""

Update

@JosefZ has rightly pointed out in their answer that the original command has an unnecessary trailing double quote ("), so although the rule is correct to get the right result the original command you are trying to escape needs to be correct. I made an assumption based off this from your question;

"I have a command that works fine with command prompt"

The original command should have been

CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep""

which using this method would be

WshShell.Run "CMD /C """"C:\Program Files (x86)\VideoLAN\VLC\VLC"" -vvv ""http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSig‌n=c2VydmVyX3RpbWU9NC‌8yNy8yMDE3IDEyO=="" :sout=#file{dst=""F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4"",no-overwrite} :sout-keep""", 0, False

and result in being executed as

CMD /C ""C:\Program Files (x86)\VideoLAN\VLC\VLC" -vvv "http://www.foo.com:8085/video.mp4/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9NC8yNy8yMDE3IDEyO==" :sout=#file{dst="F:\\Partition C Backup\\Downloads\\Video\\TESTING.mp4",no-overwrite} :sout-keep"


来源:https://stackoverflow.com/questions/43649265/run-programs-with-too-many-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!