i have a file that has a couple 100 lines in it, i only want to keep a portion of it, and discard the rest.
first i build a function that gives me the row number where the file should realy end on by using this:
for /f "tokens=1 delims=:" %%a in ('
findstr /n /c:"(search term)" "(source file location and name)"
') do set start=%%a
SET /A truestart=%start%-1
this gives me a row value that is 1 above the seached term and that is exactly what i want.
e.g. the value is 600 so it copies rows 1 to 599 to a different file and ignores row 600 to end document.
i tried looking for a solution thinking i should be using a form of loop to copy the lines 1 by one to a new file until specified line is reached.
i found this loop on this page that supposed to do what i want:
setlocal ENABLEEXTENSIONS
setlocal ENABLEDELAYEDEXPANSION
:: set counter
set c=0
for /f "delims=|" %%i in ("source file location and name") do (
:: increment counter for each line read
set /a c=!c!+1
if !c! leq %truestart% echo %%i >> "destination file location and name"
)
this does give me a new file but it copies only the source file location and name to the destination file and not the content in the source.
so i know im missing something reading out the source per line but i cant figure out what.
i also tried using more
more /e /p +%truestart% "source file location and name" > "destination file location and name"
and this gives me the opposite, this displays only the part i want to take out and leaves out what i actually want.
Laurens is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.