Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Batch file by javascript.

Status
Not open for further replies.

botatun

Programmer
Mar 7, 2006
40
US
This .bat file will run script to send bunch of files to AS400.

var url = "C:/ECommerce/CpyOrder.bat"
var WindowName = 'Run_File';
var WindowOptions =
'width=400,height=200, left=180,top=80, toolbar=no, location=no, directories=no, status=yes, scrollbars=yes,resize=no,menubar=no';

window.open(url, WindowName, WindowOptions);
or
document.location.href=url;

When I tried to open new window it said "Accessed is denied"
When I try open document in same window it said "Permission denied".

Is it possible to run .bat file by javascript?


 
If you are using Internet Explorer you can do this by using ActiveX and the FileSystemObject. It will only work in IE and executing the script will prompt the user with a security warning for the ActiveX and they will have to allow it before it happens.

If this is something you will run on your own machine then it is easier to have the file stored as a .HTA file (HTML for Applications) which is written with HTML and VBScript/JScript but executes as a client-side application with whatever rights the current logon has.

I do not have any Javascript sample code but I have some in VBScript. Or you can do a Google search for JScript FileSystemObject which should turn up some sample code.


My assumption is that you want to add a button on your own personal web page to perform the function?


It's hard to think outside the box when I'm trapped in a cubicle.
 
If you are using Internet Explorer you can do this by using ActiveX and the FileSystemObject".
May you show me some example of how to do it.
Thank you.
 
Actually, I just put together a sample in Javascript.

Code:
<html>
<head>
<script type="text/javascript">
function runApp(which) {
  WshShell = new ActiveXObject("WScript.Shell");
  WshShell.Run (which,1,true);
}
</script>
</head>
<body>
<!-- Two ways to create a link to run the app. -->
<font onClick="runApp('file://c:/winnt/notepad.exe');" style="cursor: hand;"><u>Notepad</u></font>
<br>
<!-- Or use <a> descriptor -->
<a href="runApp('file://c:/test.bat');">Batch File</a>
</body>
</html>

It's hard to think outside the box when I'm trapped in a cubicle.
 
Thank you. It is work with notepad.exe, but throw "Unable to wait for process" error. Do you know how to suppress it? Also, the .bat that has FTP script only shows in seconds and disappeared?
 
Change true to false in this line:
Code:
WshShell.Run (which,1,[COLOR=red]true[/color]);

That will stop it from giving the error when opening notepad.

As for the batch file, you have to have a bat file of that name in that location and it will only last as long as the bat file is running and the window will close automatically.
I just put a simple one line .bat file on my system to test with.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top