Okay, you can achieve this using a FOR /F loop in your batch file to process the output of your PowerShell command.
The FOR /F command is designed to parse the output of commands or files. Here's how you can integrate your PowerShell query:
@echo off
setlocal
REM --- Configuration ---
set "TARGET_DIR=C:\Users\user\Documents\scratch\xsparql\etl"
set "JAR_FILE=.\xsparql-cli-jar-with-dependencies.jar"
REM Assuming %args% is set correctly before this script runs or passed as a parameter
REM Example: set "MY_ARGS=-Dopt1=val1 -Dopt2=val2"
echo Finding and processing .xsparql files in %TARGET_DIR%...
REM Loop through the output of the PowerShell command
REM FOR /F "delims=" %%i iterates through each line of output from the command in the single quotes.
REM "delims=" ensures that lines with spaces are treated as a single token.
FOR /F "delims=" %%i IN ('powershell -NoProfile -Command "Get-ChildItem -Path '%TARGET_DIR%' -Recurse -Filter '*.xsparql' | ForEach-Object {$_.BaseName}"') DO (
echo Found basename: %%i
REM Construct and execute the Java command for each basename
REM Use %%i.xsparql and %%i.ttl to build the filenames
REM Added quotes around filenames/paths to handle potential spaces
java -jar "%JAR_FILE%" "%%i.xsparql" %args% -f "%%i.ttl"
REM Optional: Add a separator or check the exit code of the java command
echo.
)
echo Processing complete.
endlocal
Explanation:
@echo off: Prevents commands from being printed to the console.
setlocal: Keeps variable changes local to this script.
- Configuration Variables: Sets
TARGET_DIR and JAR_FILE for clarity and easier modification.
FOR /F "delims=" %%i IN ('...') DO (...): This is the core logic.
FOR /F: Command to process text input.
"delims=": Specifies that the delimiter should be nothing, meaning the entire line read from the command's output is assigned to the loop variable. This is important if your basenames could potentially contain spaces (though less common).
%%i: The loop variable. Each iteration, %%i will hold one line of output from the command. (Use %i if running directly on the command line, %%i in a batch file).
IN ('...'): The command whose standard output will be processed.
powershell -NoProfile -Command "...": Executes the specified PowerShell command.
-NoProfile: Speeds up startup slightly by not loading user profiles.
Get-ChildItem ... | ForEach-Object {$_.BaseName}: This is your refined PowerShell command. It finds the files and outputs only the base name (e.g., myfile) for each one, one per line.
DO (...): The block of commands to execute for each iteration (each basename found).
echo Found basename: %%i: Prints the basename that was found in this iteration.
java -jar "%JAR_FILE%" "%%i.xsparql" %args% -f "%%i.ttl": This is the command executed for each file.
"%JAR_FILE%": Calls the specified JAR file (quoted for safety).
"%%i.xsparql": Constructs the input filename using the basename (%%i) and the .xsparql extension. Quoted to handle spaces.
%args%: Inserts the arguments you have defined elsewhere.
-f "%%i.ttl": Specifies the output file using the basename (%%i) and the .ttl extension. Quoted to handle spaces.
echo.: Adds a blank line for readability between command outputs.
endlocal: Ends the local environment scope.
This script will find all .xsparql files recursively, extract their basenames, and then run your Java command for each one, substituting the correct basename into the input and output filenames.