Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


script to combine lines in txt file

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,58874
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> script to combine lines in txt file
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 script to combine lines in txt file - 4/7/2008 11:17:19 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
hey everybody, im a total noob when it comes to scripting.  i have done some minor work in vb and worked a lot with html php.  i dont even know if there are any similarities to the languages.  i need a script to take a text file input combine the lines in it and output to a different txt file.  i dont want to ask someone to just write it, but im a little unsure where to start.  any resources or advice would be helpful.  here is an ie of what i want it to do.

REC12400621     5928      095119      MME02/15/2008           PTGARSTEVE                    C
CARGLE                             1533 FAIRVIEW ROAD                 ELLENWOOD

REC12400621     5928      095119      MME02/15/2008           GREMP
STEVE'S TREE SERVICE               SELF EMPLOYED

would become


REC12400621     5928      095119      MME02/15/2008           PTGARSTEVE                    C CARGLE                             1533 FAIRVIEW ROAD                 ELLENWOOD

REC12400621     5928      095119      MME02/15/2008           GREMP STEVE'S TREE SERVICE               SELF EMPLOYED


there is a little more to it, but i pick up quick and if i could just get that basic effect for now i think i could handle it.  thanks in advance for any help
 
 
Post #: 1
 
 RE: script to combine lines in txt file - 4/7/2008 11:31:32 AM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
FileSystemObject - do a search for this...it will allow you to read/write to a text file

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 2
 
 RE: script to combine lines in txt file - 4/7/2008 11:35:53 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
thanks for the quick response.  i will start messing with this to see what i can come up with.  i have looked through your forums and know you generally like to have people post code then help them modify it and i really appreciate the help even though i havent really got anything yet

(in reply to chriswebb18)
 
 
Post #: 3
 
 RE: script to combine lines in txt file - 4/7/2008 11:54:51 AM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
ok so this may soun d really stupid, but im using vis studio 05.  i wokr with vb and c++ occasionally.  normally i can hit debug, or publish then run it.  umm how do i compile this, or do you not have to when you use scripts?

(in reply to chriswebb18)
 
 
Post #: 4
 
 RE: script to combine lines in txt file - 4/7/2008 12:38:24 PM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You don't compile VBScript...save the code into a text file, rename it to .vbs, and double click it.

WScript.exe will execute the .vbs by default for the most part
CScript.exe can be used to execute the script from the command line

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 5
 
 RE: script to combine lines in txt file - 4/7/2008 1:49:11 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
ok so ive got it to open the input txt to read and able to create an output txt just fine.  i know this isnt much but im still working and i am a noob.  now i want to find a way to have it recognize lines starting with "REC" followed by an 8-digit number.  ie "REC********" but im not sure if the * only represents one character or if it reps 0 or more characters.  the lines starting rec... are new lines, where every thing else needs carriage returns/line breaks removed.  if that doesnt make sense see my first post above.  thanks again for any help

' VBScript source code
dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\medctrin.txt", Forreading, True)

dim filetxt2, getname, path
Set filetxt2 = filesys.CreateTextFile("c:\medctr.txt", True)
path = filesys.GetAbsolutePathName("c:\somefile.txt")
getname = filesys.GetFileName(path)
If filesys.FileExists(path) Then
  Response.Write ("Your file, '" & getname & "', has been created.")
End If

(in reply to dm_4ever)
 
 
Post #: 6
 
 RE: script to combine lines in txt file - 4/7/2008 1:59:51 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
slightly closer i have this which will search the file and display all lines starting with the said pattern, but i am unsure how to put the output to my new txt file and have yet to try integrating it with my first bit of code which creates the output file


      

< Message edited by chriswebb18 -- 4/7/2008 4:50:09 PM >

(in reply to chriswebb18)
 
 
Post #: 7
 
 RE: script to combine lines in txt file - 4/7/2008 2:03:23 PM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Looks like you're getting closer...one suggestion would be to change your regular expression pattern to "^REC\d{8}"

Open your original for reading, open another file using the same OpenTextFile method for writing or appending, and add your lines to this file you opened for writing.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 8
 
 RE: script to combine lines in txt file - 4/7/2008 3:43:00 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
OK, so I have my input file, it can search for the patient ids then move it to a new output file with no problems,  heres what i would think to be the hardest part:  i need the other lines in as well, but without line breaks.  well no line break until the next patient id... i am also looking into a way to have the output save with the date in the name, since this is something that will have to run everyday.


      

< Message edited by chriswebb18 -- 4/7/2008 4:50:41 PM >

(in reply to chriswebb18)
 
 
Post #: 9
 
 RE: script to combine lines in txt file - 4/7/2008 3:55:59 PM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Will it always be the two lines together like in your original example?

If so, then try....(p.s. untested code below)


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 10
 
 RE: script to combine lines in txt file - 4/7/2008 4:07:06 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
the information is spread across 6-10 lines depending on the patient, actually.  i only put the 2 lines as an example and because i cannot put the other information as it is confidential.  i was looking at this link i havent tried just yet but was wondering if this concept would work if i used the REC\d{8} pattern as the flags.  im gonna try it out if you have any thoughts on how to do this let me know.  your code looks much nicer than mine as well. lol im pretty sloppy   also, what does the const forwriting 2 set in there?   Another thing that i had not originaly thought important, i thought it wouldnt really affect this much,  but there are about 40 lines before the patient ids even come in that need to be copied straight in with no modification, but the number of lines changes with the different reports each day.

ok so i tried modifying the if..then to just do a obj.write for all other lines that did not match the pattern,  or so i though.  the output is very strange, i have to be honest nothing even looks in the same order.  can you find something in my syntax, or is this something that probably wont work


      

        


                  i am getting runtime error: input past end of file"  and when i look at ouput it is skipping some of the lines in the beginning.  ok for the lines that dont have REC... it is copying every other line.  and is not putting line breaks before the REC... lines now.

< Message edited by chriswebb18 -- 4/7/2008 4:45:07 PM >

(in reply to dm_4ever)
 
 
Post #: 11
 
 RE: script to combine lines in txt file - 4/7/2008 4:49:01 PM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Every time you do a ReadLine you move on to the next line which is why you're seeing the every other line thing.

Oh... and the constant ForWriting = 2
http://msdn2.microsoft.com/en-us/library/314cz14s(VS.85).aspx

Try this and see how it works for you...I'll check tomorrow since it is time to sleep.

      

< Message edited by dm_4ever -- 4/7/2008 5:15:04 PM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 12
 
 RE: script to combine lines in txt file - 4/7/2008 5:17:58 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
thanks for all your help.  have a good one

(in reply to dm_4ever)
 
 
Post #: 13
 
 RE: script to combine lines in txt file - 4/7/2008 5:42:48 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
that last script worked very well.  i am not sure what exactly is causing the snag, but for some reason; half of the lines are going through how i want and half are not.  the initial lines before the patient ids go in are preserved right.  about half of the patients have all their info on one line, and then there are some that only the first couple lines were brought together.  I actually just found a couple where the REC is in the middle of the line and there is no break at all.  I cannot find any similarities in these occurances at this point.  I will look into it more tomorrow.  I also need to google some of your script as i dont really understand all of what you did, making it hard for me to tinker with it.  Seeing as how it's 245 I think I should turn in.  my email is [email address redacted] if you think of anything, i will try to work on it myself because even though i appreciate all the help i feel bad that youve done so much (pretty much all) of it. 

EDIT - EBGREEN - Removed email address to frustrate spam bots.

< Message edited by ebgreen -- 4/8/2008 1:13:03 AM >

(in reply to chriswebb18)
 
 
Post #: 14
 
 RE: script to combine lines in txt file - 4/8/2008 12:30:08 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline

      

OK so i think part of the problem is the
Do Until strTemp = "" or..    

because there are occassionally null/blank  lines.  i modified so it was just until objfile.atendofstream
and the result is that the script never finishes, but none of the lines starting rec... are added


      

Since there are never 2 consecutive null lines, I thought trying something like this could help to make the Loop continue even if it hits 1 null line.  I am not sure trying to find a way to make that work, tell me if this is not an efficient way of going about it.  I know the "continue loop"  is not at all correct sytax or even anything here, but it was just to show the concept

< Message edited by chriswebb18 -- 4/8/2008 12:57:26 PM >

(in reply to chriswebb18)
 
 
Post #: 15
 
 RE: script to combine lines in txt file - 4/8/2008 1:06:30 PM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Why don't you post an example of what you're dealing with? In particular those lines that give you problems.  Just enter bogus info where applicable.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to chriswebb18)
 
 
Post #: 16
 
 RE: script to combine lines in txt file - 4/8/2008 1:27:52 PM   
  chriswebb18

 

Posts: 71
Score: 0
Joined: 4/7/2008
Status: offline
here are some bogus example lines.  i believe that everything was preserved correctly.  i tried to replicate lines that werent as long so they wouldntwrap here in the forums  these txt files are normally around 10 meg so its a lot of input here