Computing.Net > Forums > Programming > batch split a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch split a file

Reply to Message Icon

Name: maxbre
Date: June 3, 2009 at 05:47:10 Pacific
OS: Windows XP
CPU/Ram: 2 gb
Subcategory: Batch
Comment:

hello

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
)
::end

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

max



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 3, 2009 at 07:58:42 Pacific
Reply:

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.

M2


0

Response Number 2
Name: maxbre
Date: June 3, 2009 at 08:18:14 Pacific
Reply:

input:
just one file named inp.met (txt file)

output:
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


0

Response Number 3
Name: IVO
Date: June 3, 2009 at 09:48:22 Pacific
Reply:

@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


0

Response Number 4
Name: maxbre
Date: June 3, 2009 at 23:50:45 Pacific
Reply:

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

max


0

Response Number 5
Name: IVO
Date: June 4, 2009 at 02:14:51 Pacific
Reply:

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

with

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

Good work and Ciao

Ivo


0

Related Posts

See More



Response Number 6
Name: maxbre
Date: June 4, 2009 at 02:32:11 Pacific
Reply:

...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!

thanks

ciao

max


0

Sponsored Link
Ads by Google
Reply to Message Icon

editing text files multiply data of a file b...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch split a file

Split txt file after a given word www.computing.net/answers/programming/split-txt-file-after-a-given-word/20129.html

Batch file to rename a file www.computing.net/answers/programming/batch-file-to-rename-a-file/14356.html

Batch Script To FTP a file www.computing.net/answers/programming/batch-script-to-ftp-a-file/17008.html