0

i am trying to write a batch program to find the first file of a given type, lets say a .txt for example, to afterwards pass it to a java program which uses the file name and path to run.

I can already find and check if a file of the type exists but i think i am getting something wrong with the set command and how to handle the parameter afterwards.

if exist *.txt (
    echo txt exists
    set file = exist *.png
) else (
    echo No txt file found
    exit
)

echo Using %file%
java -jar MyJavaProgram.jar -%file%

I would like to do it without any arguemnts i have to pass in on the command line level and i also can not change the java jar to handle this problem.

  • expect your file will be a boolean value? – Qingfei Yuan Jul 10 '19 at 15:28
  • 2
    Open a command prompt window, run `for /?` and read the output help. All you might need in your batch file is `for %%I in (*.txt) do java.exe -jar MyJavaProgram.jar "-%%I" & goto :EOF` or `for %%I in (*.txt) do if exist "%%~nI.png" java.exe -jar MyJavaProgram.jar "-%%~nI.png"`. It is unclear with which file Java executable should be run and how many *.txt or *.png files exist in directory. See also [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) – Mofi Jul 10 '19 at 16:17
  • 2
    The `Set` command does not ignore spaces so currently, you have a variable named `%file%` with a string value of `exist *.png`. The recommended syntax for the `Set` command is `Set "VariableName=VariableValue"`, so I would recommend you use that. Instead of `set file = exist *.png` use `Set "file=exist *.png"`. – Compo Jul 10 '19 at 16:22

1 Answers1

1

As the comments have alluded to, a for loop is what you're looking for here:

@echo off

setlocal enabledelayedexpansion
for %%A in (*.txt) do (
    set "file=exist %%~nA.png"
    echo Using !file!
    if exist !file! java -jar MyJavaProgram.jar -"!file!"
)

This will loop through all of the text files in your current directory and set each as parameter %%A, set the file variable as "exist nameofyourtextfile.png", echo your line, then run your .jar with your file variable. Exclamation points are needed here since delayed expansion is required to process each text file (in each loop iteration).

If you literally just want to use the first text file you come across, you can just make the loop exit with an exit or a GOTO:

@echo off

setlocal enabledelayedexpansion
for %%A in (*.txt) do (
    set "file=exist %%~nA.png"
    echo Using !file!
    if exist !file! java -jar MyJavaProgram.jar -"!file!"
    GOTO endloop    
)
endlocal

:endloop

If I've misunderstood your set statement and you don't actually want the word exist in your variable (if you just wanted the name) - you can just remove it and the space prior to %%~nA.png. If you let us know what specific output you're looking for, we can help you tweak this as necessary.

mael'
  • 420
  • 3
  • 7
  • Thank you very much. I actually just wanted the name like you explained in the comment. Sorry for the confusing set statement, i clearly have to learn about that more. I would like the window to stay open after the java program finished. I would have used a pause but i doubt it is the best was to that either – Le olde Wire Jul 10 '19 at 18:30
  • It's no problem, and feel free to through a `pause` in there after the loop - they are great for troubleshooting. – mael' Jul 10 '19 at 18:33