Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,459 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,792 people online right now. Registration is fast and FREE... Join Now!




Formatting a Text File

 
Reply to this topicStart new topic

Formatting a Text File

kirankumarvoona
5 Aug, 2008 - 12:30 PM
Post #1

New D.I.C Head
*

Joined: 29 Jul, 2008
Posts: 1

Hi,
I have attached a code below. The below code will delete first 171 lines and results in a new text file. As Line 172 contains ¨NSC : 238482 in the file i am formatting. But ¨NSC : 238482 may not be at same 172 in all the files. So , i want to delete all lines before NSC : 238482 . ¨




CODE

Const SkipLines As Integer = 171

Private Sub Form_Load()

    Dim sCmd As String
    sCmd = Command()
    
    If Trim(sCmd) = "" Then
        MsgBox "Usage: ReformatFtb.exe FILENAME"
        End
    End If
    
    ReformatFtb sCmd
End Sub


Sub ReformatFtb(sFilename As String)
Dim lCounter As Long

Dim f, f2, fs, l

Set fs = CreateObject("Scripting.FileSystemObject")                     '# create fso

fs.CreateTextFile (sFilename & ".done")                                 '# create new dest file

Set f = fs.OpenTextFile(sFilename, ForReading, TristateFalse)           '# open the source
Set f2 = fs.OpenTextFile(sFilename & ".done", ForWriting, TristateFalse)    '# open the dest

Do While f.AtEndOfStream = False
    lCounter = lCounter + 1
    l = f.ReadLine()
    
    If lCounter > SkipLines Then
        f2.WriteLine (l)                       '# write new line
    End If
Loop

f.Close                                 '# CLOSE FILES
f2.Close

End

End Sub

User is offlineProfile CardPM
+Quote Post

Zhalix
RE: Formatting A Text File
6 Aug, 2008 - 07:18 AM
Post #2

D.I.C Head
**

Joined: 7 May, 2008
Posts: 218



Thanked: 9 times
My Contributions
I don't use the FileSystemObject scripting stuff a lot so I can't tell you how to do it with that, but this is a way to do it with the Input/Output commands.

CODE
Dim strTemp As String
Dim blnNSC As Boolean
    
Open "C:\Old.txt" For Input As #1
    Open "C:\New.txt" For Output As #2
        
        Do Until EOF(1)
            
            Line Input #1, strTemp
                
            If blnNSC = False Then
                If strTemp = "NSC : 238482" Then
                    blnNSC = True
                    Print #2, strTemp
                End If
            Else
                Print #2, strTemp
            End If
                    
        Loop
        
    Close #2
Close #1


It checks one line at a time, and when it happens to be "NSC : 238482", it marks a boolean as True and starts writing it to the new text file.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:09PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month