0

How do I create a bat file to process different files within a directory? The file names are incrementing as below and the command that I need in the bat file should be something like java -jar Test.jar (the incrementing file names) one after the other. I have create one .bat file which looks like this -

java -jar Test.jar TESTDOC1.xml 
java -jar Test.jar TESTDOC2.xml
java -jar Test.jar TESTDOC3.xml

pause

But there is a possibility that the count might increase to a huge number.

File Names:

TESTDOC1.xml TESTDOC2.xml TESTDOC3.xml

how do I do it?

alexherm
  • 1,362
  • 2
  • 18
  • 31
TWEET
  • 1
  • 2
  • with the usage of a `for` loop and wildcard `*`. open cmd.exe and enter `For /?` for more information – T3RR0R Aug 20 '20 at 14:17
  • 1
    Seems like it would be better to change your JAVA program to use a wildcard to read the files from the directory. The reason I say that is because cmd.exe has a line limit of 8,192 bytes. If you have hundreds of XML files that you need to process, the command line execution could easily exceed that. – Squashman Aug 20 '20 at 14:28
  • @Squashman Thanks. What if I redirect the output to a simple .txt file. The challenge that I have here is that I am not supposed to edit the java program but instead create a .bat file to feed the files to the jar file. – TWEET Aug 20 '20 at 14:51
  • @TWEET Take a look on my answer on [write all files found in one directory into one command line?](https://stackoverflow.com/a/62359278/3074564) – Mofi Aug 20 '20 at 15:37
  • I guess I need to understand why they all need to be on the same line for execution. Why couldn't you just loop through the file names and keep executing java? – Squashman Aug 20 '20 at 19:51

2 Answers2

0

If the files are really incremented by numbers like you say, why not just:

@echo off
for /f "tokens=1,*" %%i in ('dir testdoc*.xml ^| findstr "File(s)"') do (
   for /l %%a in (1,1,%%i) do echo java -jar test.jar testdoc%%a.xml
)

Where I have echo to simply print to screen, you need to remove echo if the printed results are what you expect.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

I tried below and it worked. Thanks Squashman for the help

for /r %%i in (*.xml) do java -jar Test.jar %%i >myopt.txt
pause
Stephan
  • 53,940
  • 10
  • 58
  • 91
TWEET
  • 1
  • 2