1

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
3
  • I assume you're doing this in Internet Explorer (the only thing that supports ActiveXObject) Commented Jul 3, 2014 at 14:57
  • 3
    you need to use the /c arg for cmd.exe. without that, it wno't execute whatever you're passing in on the command line. Commented Jul 3, 2014 at 14:57
  • 2
    Do you need cmd.exe at all? Doesn't WScript.shell allow to run pict.exe directly? Commented Jul 3, 2014 at 15:00

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.