Alex k. has a good answer, which is probably suitable for most situations. (I supported.)
However, this will damage any text containing ! . This limitation can be fixed by turning the delay on and off within the cycle.
The solution is likely to be fast enough for the largest file sizes. But the FOR loop can become quite slow for large files.
I tested a 190KB file containing 2817 lines, and Alex K.'s solution took 20 seconds for one run.
Here is a completely different solution without using any cycles that process the same 190 KB file in 0.07 seconds - 285 times faster :)
@echo off setlocal enableDelayedExpansion set "file=test.txt" findstr /bv "$ &" "%file%" >"%file%.available" set "var=" <"%file%.available" set /p "var=" if defined var ( >"%file%.new" ( findstr /b "&" "%file%" <nul set /p "=&" type "%file%.available" ) move /y "%file%.new" "%file%" >nul ) del "%file%.available" echo var=!var!
Update:. As requested in the commentary, a heavily commented version of the code is presented here.
@echo off setlocal enableDelayedExpansion :: Define the file to process set "file=test.txt" :: Write the unused lines to a temporary "available" file. We don't want any :: empty lines, so I strip them out here. There are two regex search strings; :: the first looks for empty lines, the second for lines starting with &. :: The /v option means only write lines that don't match either search string. findstr /bv "$ &" "%file%" >"%file%.available" :: Read the first available line into a variable set "var=" <"%file%.available" set /p "var=" :: If var defined, then continue, else we are done if defined var ( REM Redirect output to a "new" file. It is more efficient to redirect REM the entire block once than it is to redirect each command individulally >"%file%.new" ( REM Write the already used lines to the "new" file findstr /b "&" "%file%" REM Append the & without a new line <nul set /p "=&" REM Append the unused lines from the "available" file. The first appended REM line is marked as used because of the previously written & type "%file%.available" ) REM Replace the original file with the "new" content move /y "%file%.new" "%file%" >nul ) :: Delete the temp "available" file del "%file%.available" :: Display the result echo var=!var!
I did not test this, but I just realized that I could write a line that writes available lines to look for lines starting with a character other than & :
findstr "^[^&]" "%file%" >"%file%.available"
source share