@echo off
setlocal enabledelayedexpansion

:: ==========================================
:: Configuration
:: ==========================================
:: Set the root directory to scan (defaults to the script directory)
set "input_dir=%~1"

:: Set the output filename
set "output_filename=full_report.jsonl"

:: Concatenate the full output path
set "output_file=%input_dir%%output_filename%"

:: ==========================================
:: Execution Logic
:: ==========================================
echo Scanning directory: %input_dir%
echo Output file path: %output_file%
echo.

:: Check if output file exists, delete it to avoid interference from old data
if exist "%output_file%" (
    del "%output_file%"
    echo Old output file deleted.
)

:: Initialize file count
set count=0

:: Recursive traversal
for /r "%input_dir%" %%f in (*.json) do (
    :: Exclude the output file itself
    if not "%%f"=="%output_file%" (
        set /a count+=1

        :: Store path in variable first to resolve parsing errors caused by special characters like brackets in paths
        set "current_file=%%f"

        echo [!count!] Processing: %%~nxf

        :: [Fix] Semicolon added to ensure correct command separation
        powershell -NoProfile -Command "$p='!current_file!';$c = Get-Content -LiteralPath $p -Raw -Encoding UTF8;$c -replace '[\r\n]', ''" >> "%output_file%"
    )
)

echo.
echo ==========================================
echo Processing complete!
echo Total files processed: !count!
echo Result saved to: %output_file%
echo ==========================================
