I'm trying to invoke cmd.exe from JavaScript with arguments. But it is opening cmd.exe and doing nothing.
Below is the script.
function f_InvokeEXE() {
var oShell = new ActiveXObject("WScript.shell");
var commandtoRun = '"C:\\Windows\\system32\\cmd.exe"';
var argumentString = '"C:\\Program Files (x86)\\PICT\\pict.exe" "C:\\Program Files (x86)\\PICT\\Sample.txt" > "C:\\Program Files (x86)\\PICT\\pab.xls"';
var a = oShell.run(commandtoRun + ' ' + argumentString, 1, true);
}
Its working fine when manually doing that. (opening command prompt and entering that command).
Álvaro González
147k45 gold badges282 silver badges378 bronze badges
asked Jul 3, 2014 at 14:56
Santhosh Ramini
4042 gold badges7 silver badges21 bronze badges
1 Answer 1
Use the /c arg for cmd.exe. Without that, it will NOT execute your command string:
C:\Users\marc>cmd echo foo
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\marc>exit
C:\Users\marc>cmd /c echo foo
foo
C:\Users\marc>
Note how the version WITHOUT /c just starts up a NEW shell.
answered Jul 3, 2014 at 14:59
Marc B
362k44 gold badges433 silver badges508 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Santhosh Ramini
could you please modify the function I posted as per your answer and repost it? var argumentString = 'cmd /c notepad.exe' is working but var argumentString = 'cmd /c "C:\\Program Files (x86)\\PICT\\pict.exe" "C:\\Program Files (x86)\\PICT\\Sample.txt" > "C:\\Program Files (x86)\\PICT\\pab.xls"' is giving error as "C:\Program" is not recognized as internal or external command
lang-js
ActiveXObject)/carg for cmd.exe. without that, it wno't execute whatever you're passing in on the command line.WScript.shellallow to runpict.exedirectly?