batch split a file

June 3, 2009 at 05:47:10
Specs: Windows XP, 2 gb

I want to extract some lines from a file and then redirect the output into many different files;

I've managed to jot down this code which is working fine with one input file BUT just one output file

:: start
@echo off
setLocal EnableDelayedExpansion >out.met

for /f "tokens=* delims= " %%a in (inp.met)do (
set /a N+=1
if !N! geq 1 if !N! leq 26 echo %%a >>out.met
if !N! equ 28 echo %%a >>out.met
if !N! geq 29 if !N! leq 52 echo %%a >>out.met

now I would like to generalise it so that from the input file are taken:
lines 1 to 26
line 28
lines 29+i to 52+i

where i=0, 24, 48, 72, and so on with step 24

and these lines are redirected to n output files (as many as the number of reiterations) and finally output files named with consecutive numbers

Im'm thinking to something like
for /L %%i in (29 24 8812) do (
if !N! equ %%i echo %%a ......
to add somewhere to the above code but I cannot find a proper solution...

any hint?

thank you


See More: batch split a file

Report •

June 3, 2009 at 07:58:42
I'm not quite with you. Will there be multiple input files? Or one input for which you need to 'skip by 24'?

If at first you don't succeed, you're about average.


Report •

June 3, 2009 at 08:18:14
just one file named inp.met (txt file)

many files to be named with a consecutive number *.met
(in this case: 8812 - 28 = 8784 lines from original file,
8784 lines / 24 lines/out file = 366 out files as final product)

lines 1 to 26 and line 28 of original input file (inp.met) are also stored in each output file

in other words, the "head" of the final output files is always the same while the "tail" (i.e. last 24 lines) is different

thank you

Report •

June 3, 2009 at 09:48:22
@echo off & setLocal EnableDelayedExpansion
type inp.met | find /N /V "" | find /V "[27]" > tmp.met

set idx=0
set fno=0000
set lim=28

type nul > out_0000.met
for /F "tokens=1* delims=[]" %%j in (tmp.met) do (
  if %%j gtr !lim! (
    set /A lim+=24
    set /A idx+=1
    set fno=!idx!
    if !idx! lss   10 set fno=0!fno!
    if !idx! lss  100 set fno=0!fno!
    if !idx! lss 1000 set fno=0!fno!
    type nul > out_!fno!.met
  echo.%%k>> out_!fno!.met
del tmp.met

ren out_0000.met header.met
for %%j in (out_????.met) do copy header.met + %%j %%~nj.tmp > nul
del header.met
del out_????.met
ren out_????.tmp out_????.met.
:: End_Of_Batch

Report •

Related Solutions

June 3, 2009 at 23:50:45
well ivo, it's amazing!

in that batch there is a wealth of things to study about: piping, declaration and use of dinamic variables, renaming of files with the by-pass of the "octave problem", pasting of files and so on...
Now I have something to work upon for the next few weeks!
Just one thing: at the very end of each final product file there is a symbol like 
what is it? and how can I get a rid of it ?
(because that symbol is somehow hampering my further usage of the file as input to another processing...)

thank you so much


Report •

June 4, 2009 at 02:14:51
The exoteric symbol is ASCII hex 1A that marks the end of file. To get rid of that replace

for %%j in (out_????.met) do copy header.met + %%j %%~nj.tmp > nul


for %%j in (out_????.met) do copy header.met + %%j %%~nj.tmp /B > nul

Good work and Ciao


Report •

June 4, 2009 at 02:32:11
...oh, I see now!
and now I fully inderstand also some other problems I had with the concatenation of files (in other scripting execises)

...and here again another lesson learned!




Report •

Ask Question