1

I need to run a .bat file when the user clicks a link on the webpage. The .bat file basically runs another executable file and saves its output in a text file. So what I want is to create that text file once the user clicks the link to the .bat file on the webpage. Now, my .bat file is working perfectly when I execute it separately it creates the text file with contents, but somehow when I click the link it creates an empty text file. I looked at all the paths, they are all good; I am using Batch File , I have also tried function runApp(which) { WshShell = new ActiveXObject("WScript.Shell"); WshShell.Run (which,1,true); } But both of them just create the text file, and not put the contents

Does any one has any idea about this, also is there any other way to do this, like running the original .bat file and then getting its output in a text file directly with html/javascript?

Appriciate any help Thanks

user1285
  • 51
  • 1
  • 4
  • 8

2 Answers2

2

You don't say anything about what environment you are working with and I would guess you're not working with a server-side environment. JavaScript normally works in a browser to respond to the user's clicks and mouse moves etc but strictly within the confines of the browser. What you are trying to do is perform I/O operations on the underlying OS that the browser is running in (if you are running locally) or on the server-side OS in a normal webpage environment. It's not just a security issue - JavaScript simply doesn't have any direct connection to the client's OS or the server-side OS for that matter.

What you need is a web server environment like Apache or IIS etc, probably running an environment like ASP.NET, JSP, PHP(with a nice framework like CodeIgniter), or, rather you than me, CGI.

The user clicks a link or a submit button, and sends a request to the server. The relevant server-side program processes the request, runs the I/O operation you talk about and responds with the text. JavaScript is irrelevant in most of that process. It only comes into its own again when you are trying to figure out how to display the response in some fancy dynamic way.

There are millions of tutorials out there:

Having said all that, there is a server-side JavaScript environment (http://nodejs.org/) but the point is you will always be restricted by the limitations of the http protocol which means that you send a request to a server, the server processes your request depending on your privileges as a client, performing an I/O operation if appropriate, and responds with a stream of HTML. It does not allow direct operations on the server.

None of this is easy. Expect steep learning curves.

Displaying the text file contents

Here's a sample JSP page which will read the contents of a text file then display it on the webpage. I haven't added any buttons or anything - it just demonstrates how to read a file on the server:

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BufferedReader</title>
</head>
<body>
    <%
    String path = this.getServletContext().getRealPath("/target/message.txt");
    File file = new File(path);
    FileReader reader = new FileReader(file);
    BufferedReader br = new BufferedReader(reader);
    while(br.ready()){
        out.print(br.readLine() + "<BR>");
    }
    reader.close();
    %>
</body>
</html>

/target/message.txt is the virtual absolute path (from the root of the webapp). The call to getRealPath is the way you get a real physical path that allows you to create a File object.

I'll have a look later at using exec to run a batch file, but now you're thinking of a powerful language/library like Java why do you want to run a batch file? Wouldn't it make sense to write the program in Java?

DavidHyogo
  • 2,838
  • 4
  • 31
  • 48
  • So I'm trying this in JAVA, and I'm doing this: Runtime.getRuntime().exec("cmd /c start test.bat"); but it is still not creating the text file with contents, just an empty text file :( – user1285 Jun 11 '12 at 18:01
  • Shaleen, I'm still trying to guess what environment you're in. Are you trying to run that from the command line or what? Whatever happens, if you want to initiate all this from a click on a webpage, you'll need to install Tomcat, the JSP webserver environment. Then create a suitable webpage with a clickable button which submits a request to the server. On the Tomcat server side, you will then need make the call to exec to run the .bat file and the retrieve the text file's contents. Today I was playing with the display page which I'll add to the answer. – DavidHyogo Jun 12 '12 at 11:37
  • Hi, thanks a lot for your help. I'm currently using client-side environment. I cannot write a java code for that. Basically, the issue is that I have software which has a .bat file which creates a report on some stuff. So what I need to do is to get that report and display it in a tabular format on a webpage or a window. So what I was trying to do was to run the .bat file, get its contents in a text file or in some input stream and then work on its parsing, I was using HTML and JavaScript. – user1285 Jun 12 '12 at 15:18
  • But as you said JavaScript cannot run and save output of a .bat file, that's why now I'm not sure which language to use...I even tried creating like a Java App, but again I can run the .bat file but cannot get/save the output. May be that’s because that .bat file calls another .bat file?? Thanks again!! – user1285 Jun 12 '12 at 15:18
  • First, you've got to separate in your mind client-side activity in the browser from server-side activity you're going to be pretty stuck. For instance, install the latest version of Netbeans which will have a built-in Tomcat, do some basic tutorials on how that works, then you might have a chance of getting this moving forward. It's not so much a question of languages but of understanding where those languages can operate. – DavidHyogo Jun 12 '12 at 22:25
  • It really would help to see your .bat file code. It might be easier to convert it to Java. Also, does the data collection depend on user input in any way, for instance putting the user's name at the top of the report? I'm assuming you've read advice at questions like http://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application to deal with your problem of executing the .bat file. – DavidHyogo Jun 13 '12 at 05:54
-1

use child_process.spawn of node.js

user2655757
  • 29
  • 1
  • 2
  • 6