@echo off setlocal EnableExtensions if "%~1"=="" goto usage if "%~2"=="" goto usage if "%~3"=="" goto usage if /i "%~1"=="%~3" ( echo Input and output must be different. exit /b 1 ) if not exist "%~1" ( echo Input file not found: %~1 exit /b 1 ) if exist "%~3" if /i not "%~4"=="/y" ( echo Output file exists: %~3 echo Add /y to overwrite it. exit /b 1 ) type nul > "%~3" findstr /n /i /c:"%~2" "%~1" >> "%~3" if not exist "%~3" ( echo Failed to write output. exit /b 1 ) echo Matches written to %~3 exit /b 0 :usage echo Usage: extract_matching_lines.bat input.txt pattern output.txt [/y] exit /b 2