2

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.

4
  • 4
    It's not just "frowned upon" it's downright not even a possibility in anything except IE, do you have a very specific use case and audience for this? Commented Dec 9, 2010 at 16:20
  • 1
    It's also unlikely to work even in IE, since this would represent a very nasty security hole. Here, let me download and install that malware for you... Commented Dec 9, 2010 at 16:24
  • Yes. I proposed two options to accomplish the goal, and this route was chosen. I am basically moving some files around using javascript, and then need to call a batch file afterwards. Also, I am not even using IE... The JS script runs as a post-event after a BI report runs... No one can hit a URL to execute this... Not sure why I even tried option #2 Commented Dec 9, 2010 at 16:26
  • Knowing what JS engine / application is running the script would help a bit. Not all JS environments are created equal. Commented Dec 9, 2010 at 17:12

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, so that worked when using the cscript utility. But, I don't have a way to execute the cscript utility from the javascript code... which is what I would need to do.
0

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");

Comments

Your Answer

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.