Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tebee
- Posts: 13
- Joined: 21 May 2011 08:41
#1
Post
by tebee » 28 May 2011 23:16
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
-
amel27
- Expert
- Posts: 177
- Joined: 04 Jun 2010 20:05
- Location: Russia
#2
Post
by amel27 » 29 May 2011 00:34
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"
)
)
-
amel27
- Expert
- Posts: 177
- Joined: 04 Jun 2010 20:05
- Location: Russia
#3
Post
by amel27 » 29 May 2011 20:13
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
-
tebee
- Posts: 13
- Joined: 21 May 2011 08:41
#4
Post
by tebee » 29 May 2011 23:28
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
?
Thanks again.
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#5
Post
by Ed Dyreen » 29 May 2011 23:38
type for /? in CMD, it really is worth studying.
You will then get the point hopefully.
-
amel27
- Expert
- Posts: 177
- Joined: 04 Jun 2010 20:05
- Location: Russia
#6
Post
by amel27 » 30 May 2011 03:28
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...
-
tebee
- Posts: 13
- Joined: 21 May 2011 08:41
#7
Post
by tebee » 30 May 2011 05:50
To amel27: so there is no special meaning for '$', or it has?
-
amel27
- Expert
- Posts: 177
- Joined: 04 Jun 2010 20:05
- Location: Russia
#8
Post
by amel27 » 30 May 2011 06:00
tebee wrote:so there is no special meaning for '$', or it has?
no, for convenience only