Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: batch file to split multiple text files in half.

Reply
Thread Tools Display Modes

Re: batch file to split multiple text files in half.

 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      05-07-2009

"yammyguy" <> wrote in message
news:...
>
> 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


 
Reply With Quote
 
 
 
 
Al Dunbar
Guest
Posts: n/a

 
      05-08-2009

"yammyguy" <> wrote in message
news:...
>
> 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
>



 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      05-08-2009

"yammyguy" <> wrote in message
news:...
>
> 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!


 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      05-09-2009

<> wrote in message
news:cdb09595-23d2-4756-8dd7-...
>
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Batch Script Text file parse Richard Mueller [MVP] Scripting 0 03-24-2009 04:38 PM
Re: Batch Script Text file parse Pegasus [MVP] Scripting 0 03-24-2009 04:35 PM
Re: Batch Script to parse lines in text file Al Dunbar Scripting 2 01-27-2009 09:51 PM
Re: Importing variables into Batch script from text file MMC-Michelle Scripting 2 03-05-2007 03:35 PM
Using Batch Files to delete text file on root of c: Nate B. Windows Server 1 04-11-2006 10:10 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59