batch file for runing a java command

58 views Asked by At

I have to run the following command for hundreds of .docx files in a directory in a windows in order to convert them to .txt.

java -jar tika-app-1.3.jar -t somedocfile.doc > converted.txt

I was wondering if there is any automatic way such as writing a ".bat" file to do this.

1

There are 1 answers

3
Wald On BEST ANSWER

Yes you can do it in batch. An example isn't coming to mind in order to help you but since you are running java commands anyway you can do it through java too. Here is an example of how you can be running the commands on CMD from java. Hope this helps.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


try {
        executeCMD("java -jar tika-app-1.3.jar -t " + <YOUR_DOC_FILE> + " > " + <TXT_NAME_OF_FILE> );
    }catch(Exception e){
        e.printStackTrace();
    }


private void executeCMD(String command) throws Exception{
    Process process = Runtime.getRuntime().exec(command);
    printMsg(command + " stdout:", process.getInputStream());
    printMsg(command + " stderr:", process.getErrorStream());
    process.waitFor();
    if(process.exitValue() != 0)
        System.out.println(command + "exited with value " + process.exitValue());
}


private void printMsg(String name, InputStream ins) throws Exception {
    String line = null;
    BufferedReader in = new BufferedReader(new InputStreamReader(ins));
    while((line = in.readLine()) != null){
        System.out.println(name + " " + line);
    }
}

UPDATE

Okey here is the way simpler batch way which I couldn't think of yesterday :D

for /r %%i in (*.doc) do java -jar tika-app-1.3.jar -t %%~ni > %%~ni.txt

It reads all the *.doc files in the directory it is executed, also the "~n" is in order to list only the filenames ( in your example "somedocfile" ) because otherwise it will do something like "C://...../somedocfile.doc" and changing the extension might be annoying.