0

Say I have an XML file.

<!-- File: "java.xml" -->
<java>
    <classpath>C:\path\to\my\classes\</classpath>
</java>

or

// File: "java.txt"
C:\path\to\my\classes\
C:\path\to\something\else\
C:\an\other\command\line\argument\

Is it possible to have a batch file to read a XML file or even something as simple as just an ordered list of strings in a text file separated by new lines \n. Then call a global command line tool such as java and pass it arguments found in the example file java.xml.

C:\Users>java -classpath C:\path\to\my\classes\
Joshua Barnett
  • 4,055
  • 5
  • 24
  • 52
  • Can I ask what is it for? – Aurélien Thieriot Feb 05 '14 at 11:31
  • Possible duplication of that then: http://stackoverflow.com/questions/15758814/turning-multiple-lines-into-one-line-with-comma-separated-perl-sed-awk – Aurélien Thieriot Feb 05 '14 at 11:45
  • Oh man... Is it on windows? – Aurélien Thieriot Feb 05 '14 at 11:46
  • Basically a proxy to trigger a command line tool with specified parameters. The tool I have to use can only execute files (with no parameters) and write text files. This is my way of working around those short comings by writing the parameters in a text file and executing a batch file that will in turn trigger a global command line tool and passing the parameters found in the text file. – Joshua Barnett Feb 05 '14 at 11:59

1 Answers1

1

Managed to work it out in the end using stuff I found here:

"java.txt"

C:\path\to\my\classes

"java.bat"

@echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (Presets/Scripts/java.txt) do (
    SET /A vidx=!vidx! + 1
    set var!vidx!=%%A
)
set var

java -classpath %var1%

pause
Community
  • 1
  • 1
Joshua Barnett
  • 4,055
  • 5
  • 24
  • 52
  • Does this solve the problem completely? Apparently this script will call `java` only for the first line. Did you mean it that way? Nothing in your question seems to confirm that. – Andriy M Feb 06 '14 at 13:08
  • You can have as many lines in the text file as you want and they be variable in the batch script under var1, var2, var3, and on... – Joshua Barnett Feb 06 '14 at 19:51
  • Yes, the `for` loop creates and populates them, I understand that. What I wanted to know was whether **you** actually wanted to call the `java -classpath` command for the first line (first variable) only or for all of them. Because if it's the latter, then it doesn't seem very convenient to write out all those `java -classpath %var1%`, `java -classpath %var2%` etc. in a script, especially when you don't know how many of them should be there! And you don't have to, there's an easier way: just call the command in the loop (instead of setting the vars): `java -classpath "%%A"`. – Andriy M Feb 06 '14 at 20:20
  • I was writing a GUI for an internal command-line tool that had to be doing with ExtendScript for Photoshop. ExtendScript doesn't allow for the passing of options to files it can only execute and write text files. This was my way of working around such issues. In my case the tool needed to pass three options e.g. `mytool --config %var1% --input %var2% --output %var3%` I was using java as an example because I didn't want to disclose my company's internal tool. – Joshua Barnett Feb 13 '14 at 10:29
  • Ah, I see, that makes much more sense now. The java example appears to have masked your actual intention for me, which, as I now understand, was reading items stored in separate lines of a text file and then passing them all (or at least more than one item) to a *single* command. If my understanding is now correct, it might be helpful for future readers if you clarified that bit in your question. An arbitrary command line like `mytool --option1 %line1% --option2 %line2% --option3 %line3%` (instead of the `java` one) would hide your real tool perfectly well, in my opinion. – Andriy M Feb 13 '14 at 11:39