|
| |||||||||
| Tags: batch, split, text |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| batch file to split multiple text files in half.
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! |
|
#2
| |||
| |||
| Re: batch file to split multiple text files in half.
On May 7, 2:18*pm, yammyguy <yammyguy.3rt...@DoNotSpam.com> wrote: > 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/ |
|
#3
| |||
| |||
| Re: batch file to split multiple text files in half. "yammyguy" <yammyguy.3rtvzb@DoNotSpam.com> wrote in message news:yammyguy.3rtvzb@DoNotSpam.com... > > 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 |
|
#4
| |||
| |||
| Re: batch file to split multiple text files in half.
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 |
|
#5
| |||
| |||
| Re: batch file to split multiple text files in half. "yammyguy" <yammyguy.3rucnc@DoNotSpam.com> wrote in message news:yammyguy.3rucnc@DoNotSpam.com... > > 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 > |
|
#6
| |||
| |||
| Re: batch file to split multiple text files in half. "yammyguy" <yammyguy.3rucnc@DoNotSpam.com> wrote in message news:yammyguy.3rucnc@DoNotSpam.com... > > 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! |
|
#7
| |||
| |||
| Re: batch file to split multiple text files in half.
On May 8, 2:14*am, "Pegasus [MVP]" <n...@microsoft.com> wrote: > "yammyguy" <yammyguy.3ru...@DoNotSpam.com> wrote in message > > news:yammyguy.3rucnc@DoNotSpam.com... > > > > > > > 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! 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/ |
|
#8
| |||
| |||
| Re: batch file to split multiple text files in half. <irobx.net@gmail.com> wrote in message news:cdb09595-23d2-4756-8dd7-eeb551e2c99f@r34g2000vba.googlegroups.com... > > 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? |
|
#9
| |||
| |||
| Re: batch file to split multiple text files in half.
On May 9, 1:58*am, "Pegasus [MVP]" <n...@microsoft.com> wrote: > <irobx....@gmail.com> wrote in message > > news:cdb09595-23d2-4756-8dd7-eeb551e2c99f@r34g2000vba.googlegroups.com... > > > > > 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... |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "batch file to split multiple text files in half." | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| End Multiple Processes With The Help Of Batch File | Sheenas | Operating Systems | 6 | 21-04-2011 01:53 AM |
| VBScript to split a text file evenly in three | Kazz013 | Software Development | 3 | 16-12-2010 06:35 PM |
| Editing text file using a batch file | beelow | Software Development | 8 | 09-03-2010 12:27 AM |
| DSMOD from batch and text file | Pete Jones | Active Directory | 3 | 04-03-2010 01:42 AM |
| scheduled printing of a text file using batch files or schtasks | Gadgetman | Windows XP Support | 18 | 13-06-2008 12:48 AM |