How to Run cmd.exe with parameters from javascript
Asked Answered
A

3

13

I am trying to write javascript which should run cmd.exe with a specified command line in it like this docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/edit:

I prepare a code after reading shellexecute method on microsoft site:

var objShell = new ActiveXObject("Shell.Application");
        objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1");

but it does not insert command line in cmd.exe.

Could anybody help me? Thank you in advance.

Azevedo answered 17/4, 2012 at 7:16 Comment(1)
cd C: hardly make any sense, `c:` would be enough.Choriamb
E
10

Maybe you don't have this ActiveX-control installed (or registered) in your computer.

WScript.Shell should be found in every Windows:

var run=new ActiveXObject('WSCRIPT.Shell').Run("commands to run");

If there are spaces in commands to run, you need to use double quotes.

Edit

The content below is mainly from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx

iRetVal = Shell.ShellExecute(
  sFile,
  [ vArguments ],
  [ vDirectory ],
  [ vOperation ],
  [ vShow ]
)

Let's take [vDirectory]. The documentation says: "The fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used."

This means that you have an invalid path for this argument (having .cmd.exe at the end of it). Also all examples for creating the ActiveX are like this:

var objShell = new ActiveXObject("shell.application");

Notice the lowercase in "shell.application".

And May12, thank's for asking this. I didn't know about this ActiveX control before, it seems to be very useful to me.

EDIT II

Your example works perfect in my app:

objShell.ShellExecute("cmd.exe", "cd C: C:\\cd c:\\ext_file main.exe test.txt", "C:\\WINDOWS\\system32", "open", 1);

With three exceptions:

  1. The one I mentioned early in this answer about the path

  2. Escaped \ used also in arguments.

  3. The last argument is type of number, not a string.

Extricate answered 17/4, 2012 at 7:54 Comment(6)
sorry for my english. i'll try to explain once again: i have a code - 'var shell = WScript.CreateObject("WScript.Shell"); shell.Run("C:\\WINDOWS\\system32\\cmd.exe");' as a result i receive an opened dos window with string : Microsoft Windows [Version 5.2.3790] (C) Copyright 1985-2003 Microsoft Corp. C:\Bats> I need something like: _C:\cd c:\ext_file C:\EXT_FILE>main.exe test.txt _Azevedo
Hmm... That Run-method runs commands to run straight in CMD, you should not call cmd.exe in that string, just the commands you want to execute in CMD, like you just wrote them in the commandline.Extricate
Teemu, I've seen this manual. But it doesnot help me. Eventually I need the window like that docs.google.com/file/d/0B7QHCoQDlEvKWUZSX3oxUDI2SDg/editAzevedo
Teemu, you was right. Really i didn't understand the sequence of arguments in objShell.ShellExecute().Azevedo
is there any way to get output here?Atchison
Would you use variables in the arguments?objShell.ShellExecute("cmd.exe", variable, "", "open", 1);Fantinlatour
I
0

If I understood correctly, you are only interested in calling another file with parameters. This is my example of calling another file from a shortcut or batch file

If there are no spaces in the path

    mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /c start /max C:\\Windows\\Notepad.exe',0,false);close()"

With spaces in the path. The double quote is replaced with #

    mshta.exe "javascript:new ActiveXObject('WScript.Shell').Run('cmd /v /c set a=""&call set #=!a:~0,1!&start /max C:\\!#!Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe!#!',1,true);close()"
Idiot answered 28/8, 2020 at 16:28 Comment(0)
G
-2
var objShell = new ActiveXObject("Shell.Application");
objShell.ShellExecute("cmd.exe", "C: cd C:\\pr main.exe blablafile.txt auto", "C:\\WINDOWS\\system32", "open", "1"); 

is usable

Guarino answered 27/1, 2015 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.