Josh
Josh

Reputation: 3611

How to run a batch script through javascript?

I know that this is frowned upon, but how can I run a batch file through javascript? I have tried three things:

1)

var oShell = new ActiveXObject("Shell.Application");
oShell.ShellExecute("test.bat", "","", "open", "1");

2)

document.location.href='test.bat';

3)

WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("test.bat");

Still no luck.

Thanks.

Upvotes: 2

Views: 11567

Answers (2)

Chris
Chris

Reputation: 1

I was having the hardest time to calla batch file. But this wordked for me.

var objShell = new ActiveXObject("WScript.Shell"); objShell.Run("%comspec%"+" /K CD C:\FolderWhereIHaveMyBatch & Batchfile.bat");

Upvotes: 0

KooiInc
KooiInc

Reputation: 122888

From what kind of program are you starting this? If you have a js file (say test.js) containing

WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("test.bat");

and run it using cscript test.js in the command line, it runs test.bat (if test.bat is in the same directory as test.js). It does so here at least. May be just a path problem?

In response to your comment: if cscript is not an option, you don't control the machine you're running this on? If that is the case, you could try looking at the rather old fashioned Hypertext Terminal Application, but I'm not sure it'll be what you need. Let it be clear that it isn't feasible to run ActiveX stuff via the browser (in IE you may, but the user has to permit it).

Upvotes: 1

Related Questions