Discussion:
batch file to split multiple text files in half.
(too old to reply)
yammyguy
2009-05-07 18:18:34 UTC
Permalink
Hello all,

I would first like to thank everyone who helped me with my previous
problem - you guys made my life so much easier! Now I have another
issue.

I have a directory full of approx. 2mb text files that I need to split
in half. they are .prn (text) files, and look like this: 28A41EML.PRN

I would like to split this file in to two files. the first file can
keep the original name, and the second could be something like
28A41EMA.PRN. our processing machines pickup and process any file that
has the EML.PRN associated with it.

each file contains data with each line containing data for a 5 minute
period, and each file contains 1 month's worth of data each 5 minute
interval is one line.

If anyone can help me split these, I would be very greatful! Thank you
very much in advance!
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile: http://forums.techarena.in/members/88249.htm
View this thread: http://forums.techarena.in/server-scripting/1175792.htm

http://forums.techarena.in
i***@gmail.com
2009-05-07 20:29:43 UTC
Permalink
Post by yammyguy
Hello all,
I would first like to thank everyone who helped me with my previous
problem - you guys made my life so much easier!  Now I have another
issue.
I have a directory full of approx. 2mb text files that I need to split
in half. they are .prn (text) files, and look like this:  28A41EML.PRN
I would like to split this file in to two files.  the first file can
keep the original name, and the second could be something like
28A41EMA.PRN.  our processing machines pickup and process any file that
has the EML.PRN associated with it.
each file contains data with each line containing data for a 5 minute
period, and each file contains 1 month's worth of data each 5 minute
interval is one line.
If anyone can help me split these, I would be very greatful!  Thank you
very much in advance!
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile:http://forums.techarena.in/members/88249.htm
View this thread:http://forums.techarena.in/server-scripting/1175792.htm
http://forums.techarena.in
Do they need to be exactly in half? Can it be every other line in
different file or do you need the first half in one and the second
half in another?

Here is some code that will split out every other line:

@echo
off
for /f "delims=: tokens=1,*" %%i in ('type file.txt ^|findstr /n .')
do ( echo %%j >tempfile.txt
call :oddeven %
%i
)

goto :EOF

:oddeven
set tempvar=
%1
set /a oddeven=tempvar %%
2
if {%oddeven%} == {0} type tempfile.txt >>
even.txt
if {%oddeven%} == {1} type tempfile.txt >>
odd.txt

-irobx
Home page -> http://www.irobx.net
Blog -> http://www2.irobx.net:8010/serendipity/
Pegasus [MVP]
2009-05-07 22:12:43 UTC
Permalink
Post by yammyguy
Hello all,
I would first like to thank everyone who helped me with my previous
problem - you guys made my life so much easier! Now I have another
issue.
I have a directory full of approx. 2mb text files that I need to split
in half. they are .prn (text) files, and look like this: 28A41EML.PRN
I would like to split this file in to two files. the first file can
keep the original name, and the second could be something like
28A41EMA.PRN. our processing machines pickup and process any file that
has the EML.PRN associated with it.
each file contains data with each line containing data for a 5 minute
period, and each file contains 1 month's worth of data each 5 minute
interval is one line.
If anyone can help me split these, I would be very greatful! Thank you
very much in advance!
--
yammyguy
If you wish to split each file of x lines so that x/2 lines end up in one
file and the rest ends up in the second file then this code should do it.
Make sure to create a copy of the whole folder before you test the script!
[01] sFolder = "D:\Thu"
[02] Set oFSO = CreateObject("Scripting.FileSystemObject")
[03] If oFSO.FolderExists(sFolder & "\Temp") _
[04] Then oFSO.DeleteFolder sFolder & "\Temp"
[05] oFSO.CreateFolder(oFolder.Path & "\Temp")
[06] Set oFolder = oFSO.GetFolder(sFolder)
[07]
[08] For Each oFile In oFolder.Files
[09] sName1 = oFile.ParentFolder & "\Temp\" & oFile.Name
[10] sName2 = oFile.ParentFolder & "\Temp\" _
[11] & Left(oFile.Name, Len(oFile.Name) - 5) & "L.prn"
[12] Set oTextFile = oFSO.OpenTextFile(oFile, 1)
[13] Set oOut1 = oFSO.CreateTextFile(sName1)
[14] Set oOut2 = oFSO.CreateTextFile(sName2)
[15] sText = oTextFile.Readall
[16] aText = Split(sText, VbCrLf)
[17] For i = 0 To UBound(aText) \ 2
[18] oOut1.WriteLine aText(i)
[19] Next
[20] For i = UBound(aText) \ 2 + 1 To UBound(aText)
[21] oOut2.WriteLine aText(i)
[22] Next
[23] oTextFile.Close
[24] oOut1.Close
[25] oOut2.Close
[26] Next
[27]
[28] For Each oFile In oFolder.Files
[29] oFSO.DeleteFile oFile
[30] Next
[31] Set oTemp = oFSO.GetFolder(oFolder.Path & "\Temp")
[32] For Each oFile In oTemp.Files
[33] oFSO.MoveFile oFile, oFolder.Path & "\" & oFile.Name
[34] Next
yammyguy
2009-05-08 00:07:16 UTC
Permalink
Hello Pegasus, and irobx...

thanks for the information - Pegasus, is what you posted a dos
script???

Each file has a header with a months worth of data, and each day has
288 entries - 5 minute intervals each day which gives a total of 8640
lines + 1 header (which will also have to be added to the second file)
So would like to split each file from the 1st to the 15th, then the rest
gets transferred to another file with the header.

I tried uploading a copy of the file through a text file, but it kept
failing...

I have about 800 or 900 of these files - and I need to run through each
and split the files this way. Obviously manually would take for EVER!
:S
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile: http://forums.techarena.in/members/88249.htm
View this thread: http://forums.techarena.in/server-scripting/1175792.htm

http://forums.techarena.in
Al Dunbar
2009-05-08 01:23:06 UTC
Permalink
Post by yammyguy
Hello Pegasus, and irobx...
thanks for the information - Pegasus, is what you posted a dos
script???
Each file has a header with a months worth of data, and each day has
288 entries - 5 minute intervals each day which gives a total of 8640
lines + 1 header (which will also have to be added to the second file)
So would like to split each file from the 1st to the 15th, then the rest
gets transferred to another file with the header.
I tried uploading a copy of the file through a text file, but it kept
failing...
I have about 800 or 900 of these files - and I need to run through each
and split the files this way. Obviously manually would take for EVER!
:S
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile: http://forums.techarena.in/members/88249.htm
View this thread: http://forums.techarena.in/server-scripting/1175792.htm
http://forums.techarena.in
Pegasus [MVP]
2009-05-08 06:14:26 UTC
Permalink
Post by yammyguy
Hello Pegasus, and irobx...
thanks for the information - Pegasus, is what you posted a dos
script???
Each file has a header with a months worth of data, and each day has
288 entries - 5 minute intervals each day which gives a total of 8640
lines + 1 header (which will also have to be added to the second file)
So would like to split each file from the 1st to the 15th, then the rest
gets transferred to another file with the header.
I tried uploading a copy of the file through a text file, but it kept
failing...
I have about 800 or 900 of these files - and I need to run through each
and split the files this way. Obviously manually would take for EVER!
:S
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile: http://forums.techarena.in/members/88249.htm
View this thread: http://forums.techarena.in/server-scripting/1175792.htm
http://forums.techarena.in
You're posting in a "scripting" newsgroup, not in a "batch" newsgroup, hence
I gave you a VB Script solution. While it might be possible to design a
batch solution to do the job, it is likely to run very slowly.

Here is how you test my solution:
1. Create a test folder.
2. Populate it with a few of your files.
3. Save my script as c:\yammyguy.vbs.
4. Modify line [01] to reflect te name of your test folder.
5. Remove the line numbers.
6. Save it again.
7. Double-click it to run it.

If you still have a problem, don't just write "it kept failing...". This
tells me next to nothing. Report exactly what happens and include all error
messages you see!
i***@gmail.com
2009-05-09 02:50:47 UTC
Permalink
Post by Pegasus [MVP]
Post by yammyguy
Hello Pegasus, and irobx...
thanks for the information -  Pegasus, is what you posted a dos
script???
Each file has a header with a months worth of data, and each day has
288 entries - 5 minute intervals each day which gives a total of 8640
lines + 1 header (which will also have to be added to the second file)
So would like to split each file from the 1st to the 15th, then the rest
gets transferred to another file with the header.
I tried uploading a copy of the file through a text file, but it kept
failing...
I have about 800 or 900 of these files - and I need to run through each
and split the files this way.  Obviously manually would take for EVER!
:S
--
yammyguy
------------------------------------------------------------------------
yammyguy's Profile:http://forums.techarena.in/members/88249.htm
View this thread:http://forums.techarena.in/server-scripting/1175792.htm
http://forums.techarena.in
You're posting in a "scripting" newsgroup, not in a "batch" newsgroup, hence
I gave you a VB Script solution. While it might be possible to design a
batch solution to do the job, it is likely to run very slowly.
1. Create a test folder.
2. Populate it with a few of your files.
3. Save my script as c:\yammyguy.vbs.
4. Modify line [01] to reflect te name of your test folder.
5. Remove the line numbers.
6. Save it again.
7. Double-click it to run it.
If you still have a problem, don't just write "it kept failing...". This
tells me next to nothing. Report exactly what happens and include all error
messages you see!
Your description is a bit confusing. So you need to copy the 1st line
into both files then lines 2-4321 to one and 4322-8641 into the other
file?

I have to agree with yammyguy that batch processing is going to be too
slow when processing thousands of lines of text like this. Though find
his commenting on scripting a bit contentious. NT shell scripting ( or
batch file programming, if you like) is very powerful and has its
place. And this is not the vbscripting group, that group is
microsoft.public.scripting.vbscript.

Yammyguy's code does the bulk of what you need but it doesnt seem to
handle the header line. If you need help with that just holla.

-irobx
Home page -> http://www.irobx.net
Blog -> http://www2.irobx.net:8010/serendipity/
Pegasus [MVP]
2009-05-09 05:58:39 UTC
Permalink
Post by Pegasus [MVP]
If you still have a problem, don't just write "it kept failing...". This
tells me next to nothing. Report exactly what happens and include all error
messages you see!
Your description is a bit confusing. So you need to copy the 1st line
into both files then lines 2-4321 to one and 4322-8641 into the other
file?

================

I assume you're replying to the OP, not to me?
i***@gmail.com
2009-05-11 12:49:18 UTC
Permalink
Post by i***@gmail.com
Post by Pegasus [MVP]
If you still have a problem, don't just write "it kept failing...". This
tells me next to nothing. Report exactly what happens and include all error
messages you see!
Your description is a bit confusing. So you need to copy the 1st line
into both files then lines 2-4321 to one and 4322-8641 into the other
file?
================
I assume you're replying to the OP, not to me?
Sorry I got the names backwards...
RandirRandwa
2009-06-01 16:28:03 UTC
Permalink
Just so that one can evaluate if batch or script is the right approach,
here is another script in biterscripting ( http://www.biterscripting.com
) .


Code:
--------------------
# Get a list of .PRN files
var str list ; lf -rn "*.PRN" > $list
# Process files one by one
while ($list <> "")
do
var str file ; lex "1" $list > $file
# Read file into a variable.
var str content ; cat $file > $content
# How many lines does the file have ?
var int lines ; set $lines = { len -e $content } ; set $lines = $lines/2
# Separate the two halves.
var str half1, half2 ; lex (makestr(int($lines))+"]") $content > $half1 ; set $half2 = $content
# Write the two halves.
echo $half1 > $file ; var str file2 ; set $file2=$file+"_2" ; echo $half2 > $file2
done
--------------------



Randir
--
RandirRandwa
------------------------------------------------------------------------
RandirRandwa's Profile: http://forums.techarena.in/members/92956.htm
View this thread: http://forums.techarena.in/server-scripting/1175792.htm

http://forums.techarena.in
Loading...