Page 1 of 1

Scanning file in blocks of lines

Posted: 28 May 2011 23:16
by tebee
Hi,
i need to break down a long file in chunks of fixed items.
I.e, if file test.txt have 906 lines, i need to have 23 blocks, 22 of 40 lines and 1 of 26 lines.
Using:

Code: Select all

set /a count=0
del tmp
for %%g in (test.txt) do (
   echo %%g>>tmp
   set /a count+=1
   if !count! equ 40 (
    set /a block+=1
    <job process with tmp file>
    del tmp
    set /a count=0
  )
)

i need to repeat a last

Code: Select all

if exist tmp (
   set /a block+=1
   <job process with tmp file>
   set /a count=0
   del tmp
)


Do there is any elegant code better that this?

Thanks

Re: Scanning file in blocks of lines

Posted: 29 May 2011 00:34
by amel27
sample 1:

Code: Select all

@echo off

set "bmax=40"
set "file=test.txt"
set "fline=0"

del tmp
for /f "usebackq delims=" %%a in ("%file%") do (set "$a=%%a"
  SETLOCAL EnableDelayedExpansion
  set/a "bline=1+fline%%%bmax%,block=1+fline/%bmax%,fline+=1"
    (echo/
     echo ---
     echo !$a!
     echo ---
     echo line in file : !fline!
     echo block number : !block!
     echo line in block: !bline!
    )>>tmp
  for /f "tokens=1-3" %%x in ("!fline! !block! !bline!") do (
    ENDLOCAL
    set/a "fline=%%x,block=%%y,bline=%%z"
  )
)

sample 2:

Code: Select all

@echo off

set "bmax=40"
set "file=test.txt"
set "fline=0"

for /f "usebackq delims=" %%a in ("%file%") do (set "$a=%%a"
  SETLOCAL EnableDelayedExpansion
  set/a "bline=1+fline%%%bmax%,block=1+fline/%bmax%,fline+=1"
  echo/!$a!>>tmp
  if !bline! equ %bmax% (
    rem <job process with tmp file>
    del tmp
  )
  for /f "tokens=1-3" %%x in ("!fline! !block! !bline!") do (
    ENDLOCAL
    set/a "fline=%%x,block=%%y,bline=%%z"
  )
)

Re: Scanning file in blocks of lines

Posted: 29 May 2011 20:13
by amel27
Oh, sorry... in examle 2 last block not processed,
but we can move common code to procedure:

Code: Select all

@echo off

set "bmax=40"
set "file=test.txt"
set "fline=0"

for /f "usebackq delims=" %%a in ("%file%") do (set "$a=%%a"
  SETLOCAL EnableDelayedExpansion
  set/a "bline=1+fline%%%bmax%,block=1+fline/%bmax%,fline+=1"
  echo/!$a!>>tmp
  if !bline! equ %bmax% call:BlockProc
  for /f "tokens=1-3" %%x in ("!fline! !block! !bline!") do (
    ENDLOCAL
    set/a "fline=%%x,block=%%y,bline=%%z"
  )
)
if %bline% lss %bmax% call:BlockProc

GoTo:EOF

:BlockProc
 rem <job process with tmp file>
 del tmp
GoTo:EOF

Re: Scanning file in blocks of lines

Posted: 29 May 2011 23:28
by tebee
Thanks amel27, effectively i was watching code wandering how i miss something, i will try soon as it looks promising as it has a call to process instead of embedding it.
As asked on the other topic on white spaces, here again i feel i have to learn how escapes works in dos/cmd batch.
I.e, what's the meaning of

Code: Select all

"$a=%%a"
?

Thanks again.

Re: Scanning file in blocks of lines

Posted: 29 May 2011 23:38
by Ed Dyreen
type for /? in CMD, it really is worth studying. :wink:
You will then get the point hopefully.

Re: Scanning file in blocks of lines

Posted: 30 May 2011 03:28
by amel27
tebee wrote:what's the meaning of
set "$a=%%a"
set content of current text line (in FOR loop) to variable $a

P.S. sorry, but my bad English doesn't allow me to be got involved in long discussions... :(

Re: Scanning file in blocks of lines

Posted: 30 May 2011 05:50
by tebee
To amel27: so there is no special meaning for '$', or it has?

Re: Scanning file in blocks of lines

Posted: 30 May 2011 06:00
by amel27
tebee wrote:so there is no special meaning for '$', or it has?
no, for convenience only