This is a great use case for PowerShell's pipeline and ForEach-Object cmdlet. The key is to iterate over the BaseNames you extract and then construct and execute your java command for each one.
Here's how you can achieve this within a batch file, along with explanations and best practices for quoting and path handling:
@echo off
setlocal
:: Define the path to your .xsparql files
set "ETL_SOURCE_PATH=C:\Users\user\Documents\scratch\xsparql\etl"
:: Define any arguments you want to pass to your xsparql-cli.jar
:: IMPORTANT: If %ARGS% contains spaces, it will be passed as a single string
:: to PowerShell. If your Java application expects multiple arguments, you
:: might need to split this string *within PowerShell* using something like
:: ($cliArgs -split ' '). For this example, we'll assume it's one block
:: or doesn't require splitting.
set "ARGS=-verbose --some-option value"
:: Define the path to your JAR file relative to where you run the batch file
set "JAR_PATH=.\xsparql-cli-jar-with-dependencies.jar"
echo Running SPARQL transformation...
echo Source Path: %ETL_SOURCE_PATH%
echo Jar Path: %JAR_PATH%
echo CLI Arguments: "%ARGS%"
echo.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& { ^
param($cliArgs, $jarPath) ^
Get-ChildItem -Path '%ETL_SOURCE_PATH%' -Recurse -Filter '*.xsparql' | ForEach-Object { ^
$baseName = $_.BaseName; ^
$inputXsparqlPath = Join-Path $_.Directory.FullName \"$baseName.xsparql\"; ^
$outputTtlPath = Join-Path $_.Directory.FullName \"$baseName.ttl\"; ^
^
Write-Host \"-- Processing: $baseName.xsparql --\"; ^
Write-Host \" Input: $inputXsparqlPath\"; ^
Write-Host \" Output: $outputTtlPath\"; ^
^
# Construct the command for Java.
# We use an array of strings for 'java.exe' arguments.
# This is generally safer than Invoke-Expression for external programs
# as it correctly handles spaces in paths/arguments.
$javaCommandArgs = @( ^
\"-jar\"; ^
$jarPath; ^
$inputXsparqlPath; ^
$cliArgs; ^
\"-f\"; ^
$outputTtlPath ^
); ^
^
# Filter out any empty/null arguments (e.g., if $cliArgs is empty)
$javaCommandArgs = $javaCommandArgs | Where-Object { $_ -ne $null -and $_ -ne '' }; ^
^
Write-Host \" Executing: java \" + ($javaCommandArgs -join ' '); ^
^
# Execute the Java command directly. '&' is the call operator.
# '@javaCommandArgs' splats the array into individual arguments.
& java.exe @javaCommandArgs; ^
^
if ($LASTEXITCODE -ne 0) { ^
Write-Error \"Java command failed for $baseName.xsparql with exit code $LASTEXITCODE\"; ^
} ^
Write-Host \"\"; ^
} ^
} \"%ARGS%\" \"%JAR_PATH%\""
if %ERRORLEVEL% NEQ 0 (
echo.
echo An error occurred during PowerShell execution.
) else (
echo.
echo All .xsparql files processed successfully.
)
endlocal
goto :eof
Explanation:
@echo off and setlocal: Standard batch file practices. setlocal ensures variables are temporary.
- Batch Variables (
set "VARNAME=VALUE"):
ETL_SOURCE_PATH: The directory to search for .xsparql files.
ARGS: Your additional arguments for the java command. This variable's content will be passed directly into the PowerShell command string.
JAR_PATH: The path to your Java .jar file.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& { ... }":
powershell.exe: Invokes PowerShell.
-NoProfile: Prevents PowerShell from loading user profiles, which can speed up execution and prevent unintended side effects.
-ExecutionPolicy Bypass: Allows the script to run without requiring a signed script (useful for local scripts). Use with caution in production environments.
-Command "& { ... }": Executes the enclosed script block. The & (call operator) ensures the script block is executed.
^: The caret is the batch file's line continuation character. It allows you to write a single long command across multiple lines for readability.
param($cliArgs, $jarPath) (PowerShell Parameter Block):
- This is how you pass arguments from the batch file into the PowerShell script block.
%ARGS% from the batch file becomes $cliArgs inside PowerShell.
%JAR_PATH% from the batch file becomes $jarPath inside PowerShell.
- Notice how
%ARGS% and %JAR_PATH% are placed after the closing brace } of the PowerShell script block and are enclosed in double quotes. This ensures they are passed as literal strings.
Get-ChildItem -Path '%ETL_SOURCE_PATH%' -Recurse -Filter '*.xsparql':
- This is your original command to find the files.
- I've used the batch variable
%ETL_SOURCE_PATH% here. It's enclosed in single quotes ' ' within the PowerShell command to prevent PowerShell from trying to interpret it as a PowerShell variable if it started with $. The batch interpreter expands it PowerShell sees the command.
This approach is robust, readable, and handles paths and arguments correctly, especially with spaces, which is often a challenge when mixing batch and PowerShell commands.