the trick is simple.
I'm using temporary file istead execute the findstr in the for /F
added:
Code: Select all
set "tmp_file=%tmp%\slice_%random%_%random%.dat"
Findstr /n "^" "%~dpf1">%tmp_file%
and substitute this
Code: Select all
For /f "delims=" %%$ In ('Findstr /n "^" "%~dpf1"') Do (
with:
Code: Select all
For /f "delims=" %%$ In (%tmp_file%) Do (
The for /f for bigger file is more slow if use the execute command. I think that the buffering is more expensive.
My file of 16MB has about 250.000 lines. (<< 2.147.483.647)
I'm studing for fast seek that is the other problem of slice.
maybe just use a simple "skip" in for.
this version has very fast seek
Code: Select all
@Echo Off
:: Usage: slice.cmd filename [<int>start] [<int>end]
:: Note: if end is 0 then prints until the end of file
:: Add trick for bigger files and fast seek
Setlocal EnableExtensions DisableDelayedExpansion
Set /A b=%~2+0,e=%~3+0,#=0 2>Nul
If %b% Leq 0 Set "b=1"
If %e% Leq 0 Set "e=2147483647"
rem using temporary file for faster execute on big file in size
set "tmp_file=%tmp%\slice_%random%_%random%.dat"
Findstr /n "^" "%~dpf1">%tmp_file%
rem fast seek
set /A #=%b%-1
For /f "skip=%#% delims=" %%$ In (%tmp_file%) Do (
Set /A #+=1 & Set "$=%%$"
Setlocal EnableDelayedExpansion
Set "$=!$:*:=!"
If !#! Geq %b% Echo(!$!
If !#! Geq %e% (Endlocal & Goto End)
rem debug on title
if !random! geq 32700 title !#!
Endlocal
)
:End
Endlocal & Goto:Eof
EDIT:
this trick can be used for reading binary(any content) files too!
In this case the limit of 2147483647 is more important.
I'm studing the trick of Sponge Belly in
this post for truncate a file / split and
thiseinstein1969