• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1827
  • Last Modified:

Delete Extra lines in a text file using VBScript

Hey Folks!

Maybe this is a simple question....
I'm using something like this to read  a texfile and insert some text:
                   Set objlines = CreateObject("System.Collections.ArrayList")
                    While Not objInputFile.AtEndOfStream
                    strLine = objInputFile.ReadLine
and so so... the thing is that I'm not sure that when I use:
                    objlines.Insert objlines.Count - 25, mytext
to insert my text of 5 lines, this command actually appends to my output file in memory...
BTW, I'm using this to write my output:
                       For Each objLine In objlines
                               objOutputFile.WriteLine objLine
                        Next

So base on that, if my original text file was 25 lines long now is 30 lines, correct? If so, how do I get to delete those 5 empty lines extra that now I don't want so my file is back to 25 lines for printing purposes?

Thanks In Advance

angelarmando
0
angelarmando
Asked:
angelarmando
  • 7
  • 7
1 Solution
 
TakedaTCommented:
Im a little confused as to where the extra lines came from to begin with.  Could you post more of the code?  If they are just empty lines, when you write them, you could probably test for the emptiness first like so.

For Each objLine In objlines
        If objLine <> "" then objOutputFile.WriteLine objLine
Next
0
 
angelarmandoAuthor Commented:
Hey TakedaT,

Sure! No Problem. I will tomorrow morning from my other workstation. Maybe you can clarify me this in the meantime:
For Example, When you use this line:
objlines.Insert objlines.Count - 15, mytext
that will insert "mytext" on line 10 on a text file that holds 25 lines of data....If you write an outputfile the usual way, do you finish with 25 lines + the ones from "mytext"? Or those inserted lines will replace some inside the original file?
0
 
TakedaTCommented:
OK.  I think I know what you are looking for now.  When you said "those 5 empty lines", I thought you were looking for blank lines.  Anyways, what I think you are looking for is something different.  You want to actually replace lines, not insert them.  The "objlines.Insert objlines.Count - 15, mytext
" line will insert a line pushing everything after that line down a notch.  Instead of this method, I would just test the readline each time to see if the line matches the line number that you want to replace, and if it does, then replace it.  Here is an example.

Const forReading = 1, forWriting = 2, forAppending = 8
 
mytext = "NewLine"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLines = CreateObject("System.Collections.ArrayList")
 
Set objInputFile = objFSO.OpenTextFile("D:\test\document.txt",forReading)
Set objOutputFile = objFSO.OpenTextFile("D:\test\output.txt",forWriting)
 
While Not objInputFile.AtEndOfStream
	strLineNum = objInputFile.Line
	strLine = objInputFile.ReadLine
	Select Case strLineNum
		Case "5","10","15","20"
			objLines.Add mytext
		Case else
			objLines.Add strLine
	End Select
Wend
 
For Each objLine In objLines
	objOutputFile.WriteLine objLine
Next

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
angelarmandoAuthor Commented:
Hey TakedaT,

That is give me plenty of ideas! Thanks..
I try to implement a solution that will do this:
Read the file to memory,
Append the "Text" into memory,
Write a new file,
read the new file,
Look for the last line of the "Text",
Jump the reader the # of lines of the "Text",
Write a new file.

Now, I tried it and when I read the second time (to the file with the "text") is bringing me a different stringline that look weird. When I read the file for the first time it looks like this:
"Bla Bla Bla"
When I read the outputfile of the first one with another reader it looks like this:
"Bla  Bla  Bla"
Any Ideas? Here is the code fo the WHOLE Script.. Sorry for the mess!

Angel
'Test Script to add the audit patient footer V5
 
'reading tools
Const intForWriting = 2
Const intForReading = 1
 
'read the footers into variables
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SecondobjFSO = CreateObject("Scripting.FileSystemObject")
 
'BH
strFooter = "C:\VBScripts\BHFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
bhfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'SMH
strFooter = "C:\VBScripts\SMHFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
smhfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'DH
strFooter = "C:\VBScripts\DHFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
dhfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'HH
strFooter = "C:\VBScripts\HHFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
hhfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'MH
strFooter = "C:\VBScripts\MHFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
mhfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'BOC
strFooter = "C:\VBScripts\BOCFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
bocfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'MARC in BH
strFooter = "C:\VBScripts\MARCFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
marcfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'MARC in SMH
strFooter = "C:\VBScripts\SMHMARCFooter.txt" 'footer to add at the last page of a patient
Set Footer = objFSO.OpenTextFile(strFooter, intForReading, True)
On Error Resume Next 'in case it has blank lines
smhmarcfooter = Footer.Readall
On Error GoTo 0
Footer.Close
 
'Process each Entity Folders base by date
 
Dim currentyear
Dim currentmonth
Dim currentday
Dim objFile
Dim fdlChild
 
currentyear = DatePart("yyyy", Now())
currentmonth = DatePart("m", Now())
If currentmonth < 10 Then
    currentmonth = "0" & currentmonth
End If
currentday = DatePart("d", Now())
 
'reading folder structure
 
strFolder = "C:\VBScripts\Reports\Patient Financial Services\Invision"
Set objFldr = objFSO.GetFolder(strFolder)
For Each fdlChild In objFldr.subfolders
    If Len(fdlChild.Name) < 5 Then
        strFolder = "C:\VBScripts\Reports\Patient Financial Services\Invision\" & fdlChild.Name & "\Archive\" & currentyear & currentmonth & currentday & "\"
        Set objFldr = objFSO.GetFolder(strFolder)
        If Not objFSO.FolderExists("C:\VBScripts\Reports\Patient Financial Services\Invision\" & fdlChild.Name & "\Archive\" & currentyear & currentmonth & currentday & "\" & "AuditFilesProcessed") Then
           Set newfolder = objFSO.CreateFolder("C:\VBScripts\Reports\Patient Financial Services\Invision\" & fdlChild.Name & "\Archive\" & currentyear & currentmonth & currentday & "\" & "AuditFilesProcessed")
        End If
        
                
        x = 0
        For Each objFile In objFldr.Files
            strinputfile = strFolder & objFile.Name
                        
            'define the right footer
        Select Case fdlChild.Name
                    Case "SMH"
                        myfooter = smhfooter
                        pageline = "                         P.O. BOX 025615                                          1"
                    Case "BHM"
                        myfooter = bhfooter
                        pageline = "                         P.O. BOX 025333                                          1"
                    Case "HH"
                        myfooter = hhfooter
                        pageline = "                          P. O. BOX 025440                                        1"
                    Case "MASC"
                        myfooter = marcfooter
                        'pageline = INSERT HERE MASC PAGELINE
                    Case "BOS"
                        myfooter = bocfooter
                        pageline = "                          P.O. BOX 025528                                         1"
                    Case "MH"
                        myfooter = mhfooter
                        pageline = "                         P.O. BOX 025819                                          1"
                    Case "DH"
                        myfooter = dhfooter
                        'pageline = INSERT HERE DH PAGELINE
                    Case "SMASC"
                        myfooter = smhmarcfooter
                        'pageline = INSERT HERE SMASC PAGELINE
        End Select
            
            If Right(strinputfile, 4) = ".txt" Then
                'Look for the AUDIT Files
                If Left(objFile.Name, 5) = "AAUDT" Or _
                Left(objFile.Name, 5) = "IAUDT" Or _
                Left(objFile.Name, 5) = "OAUDT" Or _
                Left(objFile.Name, 6) = "AFAUDT" Or _
                Left(objFile.Name, 6) = "IFAUDT" Or _
                Left(objFile.Name, 6) = "OFAUDT" Then
                    boolFoundFile = True
                End If
                
                'First Edit-Add the text footers
                If boolFoundFile = True Then
                    boolFoundFile = False
                    Set objInputFile = objFSO.OpenTextFile(strinputfile, intForReading, False)
                    boolFirstCustomer = True
                    boolNoData = True
                    Set objlines = CreateObject("System.Collections.ArrayList")
                    While Not objInputFile.AtEndOfStream
                    strLine = objInputFile.ReadLine
                    If Len(strLine) = 83 Then
                            If strLine = pageline Then
                                    If boolFirstCustomer = False Then
                                        objlines.Insert objlines.Count - 25, myfooter
                                    Else
                                        boolFirstCustomer = False
                                        boolNoData = False
                                    End If
                            End If
                    End If
                    If Len(strLine) = 44 Then
                            If Left(strLine, 21) = "     BILL FORMS COUNT" Then
                                objlines.Insert objlines.Count - 25, myfooter
                                boolDeleteExtraLines = True
                            End If
                    End If
                    objlines.Add strLine
                    Wend
                    objlines.Add strFooter
                    objInputFile.Close
                    Set objInputFile = Nothing
                    If boolNoData = False Then
                        strOutputFile = strFolder & "AuditFilesProcessed\" & objFile.Name
                        Set objOutputFile = objFSO.CreateTextFile(strOutputFile, intForWriting, True)
                        For Each objLine In objlines
                               objOutputFile.WriteLine objLine
                        Next
                        objOutputFile.Close
                        Set objOutputFile = Nothing
                        
                        'Second Edit-deleting the extra lines
                        strinputfile = strOutputFile
                        Set objSecondInputFile = SecondobjFSO.OpenTextFile(strinputfile, intForReading, False)
                        Set Secondobjlines = CreateObject("System.Collections.ArrayList")
                        While Not objSecondInputFile.AtEndOfStream
                        SecondstrLine = objSecondInputFile.ReadLine
                        starline = "***********************************************************************************"
                        If SecondstrLine = starline Then
                            If boolfirststarline = no Then
                                boolfirststarline = yes
                            Else
                                For skipline = 0 To 4
                                    Secondobjlines.Add SecondstrLine
                                Next
                            End If
                        End If
                        Secondobjlines.Add SecondstrLine
                        Wend
                        Secondobjlines.Add strFooter
                        objSecondInputFile.Close
                        Set objInputFile = Nothing
                        Set objSecondOutputFile = SecondobjFSO.CreateTextFile(strOutputFile, intForWriting, True)
                        For Each SecondobjLine In Secondobjlines
                               objSecondOutputFile.WriteLine SecondobjLine
                        Next
                        objSecondOutputFile.Close
                        Set objSecondOutputFile = Nothing
                    End If
                End If
            End If
        Next
    End If
Next
 
MsgBox "Done"

Open in new window

0
 
TakedaTCommented:
Thats a lot of code.  Im sorry, but its too difficult to see what you are trying to do without seeing the files you are reading and what you are trying to replace.  
0
 
angelarmandoAuthor Commented:
Ok. Ignoring the code, Can you read a write twice on the same script using this for example?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SecondobjFSO = CreateObject("Scripting.FileSystemObject")
the script is doing his job, it just the when you read the second time you are bringing something weird in my Editor Watch.
Like I said before, I try to implement a solution that will do this:
Read the file to memory,
Append the "Text" into memory,
Write a new file,
read the new file,
Look for the last line of the "Text",
Jump the reader the # of lines of the "Text",
Write a new file.
Is that possible?
0
 
TakedaTCommented:
Im not sure what you mean by "read a write". Im also not sure why you would need a second FSO object. You can open multiple files with just the one instance. When you said "read the second time", what exactly do you mean. You are reading the same file twice?
I think you problem lies elsewhere, I translated the "Bla  Bla  Bla" line into hex and those box characters are showing as ascii character 01 as below, which is a SOH or start of header character.


220142016C01612001200142016C01612001200142016C016122
Can you show me the section of code where this is happening?  Ill see what I can find.


0
 
angelarmandoAuthor Commented:
Hi TakedaT,

First of all, thanks for you help and your effort in this :P

Ok. I just simplify a version just to recrete this only. Please see attach, extact the folder to your C: so you can test it. I also attach the simple code version here.
Now, my question is why I'm getting the gibbeish in the "Proccessed" subfolder after I read and write twice? Any idea?
How can I make this work so the output is the same?
HINT: Put some watches on the different strlines and you will see what I'm talking about ;P

angelarmando
'Test Script
 
'reading tools
Const intForWriting = 2
Const intForReading = 1
 
'read the footers into variables
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SecondobjFSO = CreateObject("Scripting.FileSystemObject")
 
 
'reading folder structure
 
strFolder = "C:\VBScriptstest\"
Set objFldr = objFSO.GetFolder(strFolder)
 
        
        x = 0
        For Each objFile In objFldr.Files
            strinputfile = strFolder & objFile.Name
              
                'First Read
                
                    Set objInputFile = objFSO.OpenTextFile(strinputfile, intForReading, False)
                    Set objlines = CreateObject("System.Collections.ArrayList")
                    While Not objInputFile.AtEndOfStream
                    strLine = objInputFile.ReadLine
                    objlines.Add strLine
                    Wend
                    objInputFile.Close
                    Set objInputFile = Nothing
                        strOutputFile = strFolder & "Processed\" & objFile.Name
                        If Not objFSO.FolderExists("C:\VBScriptstest\Processed") Then
                              Set newfolder = objFSO.CreateFolder("C:\VBScriptstest\Processed")
                        End If
                        Set objOutputFile = objFSO.CreateTextFile(strOutputFile, intForWriting, True)
                        For Each objLine In objlines
                               objOutputFile.WriteLine objLine
                        Next
                        objOutputFile.Close
                        Set objOutputFile = Nothing
                        
                        'Second Read
                        strinputfile = strOutputFile
                        Set objSecondInputFile = SecondobjFSO.OpenTextFile(strinputfile, intForReading, False)
                        Set Secondobjlines = CreateObject("System.Collections.ArrayList")
                        While Not objSecondInputFile.AtEndOfStream
                        SecondstrLine = objSecondInputFile.ReadLine
                        Secondobjlines.Add SecondstrLine
                        Wend
                        Secondobjlines.Add strFooter
                        objSecondInputFile.Close
                        Set objInputFile = Nothing
                        Set objSecondOutputFile = SecondobjFSO.CreateTextFile(strOutputFile, intForWriting, True)
                        For Each SecondobjLine In Secondobjlines
                               objSecondOutputFile.WriteLine SecondobjLine
                        Next
                        objSecondOutputFile.Close
                        Set objSecondOutputFile = Nothing
        Next
 
MsgBox "Done"

Open in new window

VBScriptstest.zip
0
 
TakedaTCommented:
Yea...I see what you are talking about.  Im not sure quite why though.  It may be that the script hasnt released the file for it to be read.  Maybe you cant read a file you just created in the same script.  But, if all you are doing writing the same thing that is in the first file, then why not just do a file copy instead?
0
 
angelarmandoAuthor Commented:
Good! Now you notice. ;P
Well... the purpose of reading the file twice is that I want to eliminate the extra lines that I have added with the insertion of those 5 lines in the first read. I was looking to "look for them" in the second read and move the objectInputFile.Readline 5 times so I can skip some empty lines after those and then write for the last time a file without them...
Now, can we confirm that we can't read a write the same file in one script? If so, I will have to make a second script just for that and run a batch file ;P....

Thanks for everything,

angelarmando
0
 
TakedaTCommented:
I see what you are trying to do.  Instead of writing to a new file and reading the new file, put the lines in 1 long string, then parse the string line by line instead.  Then you will only have to do one write and you problem is gone.  You can parse each line by looking for the carriage return/line feed.  Its a bit more complicated code wise, but it should fix your problem.
0
 
angelarmandoAuthor Commented:
I like the Idea! But I'm afraid to ask...
Can you give me some a code example tha tI can modify and insert in my original Script?
0
 
angelarmandoAuthor Commented:
Hey TakedaT. I finally made a nice research about this. This problem happens since my files are coming from mainframes. They use another way to "binary" way to write those files. I will give you the points and post another question about this.
0
 
TakedaTCommented:
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now