Results 1 to 16 of 16

Thread: Anyone good with Text Files

  1. #1

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Unhappy

    Help!!

    I need some help munipulating text files.

    I have to write a program that does the following:

    I have three text files ( X,Y,Z ). I want to write the data within these files to a new text file. This new text file has three headers. I want the data from X File under the first header, the data from Y file under the second and so on.

    I also want to manipulate the format of the text files to comma seperated value before they get placed into my new text file.

    I have not done that much coding with text files, so all help will be very much appreciated.

    Regards

    Mark

  2. #2
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181

    Wink

    what do you mean by
    I also want to manipulate the format of the text files to comma seperated value before they get placed into my new text file.

    give an example!

    the other stuff seems easy to me.
    Just read the X file
    put it out to the Final fine dont close the final file
    put out the next header
    Just read the y file
    put it out to the Final fine dont close the final file
    ...
    Sanity is a full time job

    Puh das war harter Stoff!

  3. #3

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Unhappy Manipulation

    Could you give me an example of some code to do as you mentioned. I know how to open a file but thats it. With regards to manipulating files. The files ( X,Y,Z ) are going to be delimited and i need to change this to CSV.

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    pseudo code like this:


    Dim iTextFile As Integer
    Dim iNewFile As Integer


    iNewFile = FreeFile 'Open new file
    Open YourNewFile For Append As #iNewFile 'Open to allow us to append.

    'for YourFileX to YourFileZ

    iTextFile = FreeFile 'Get handle of Read file.
    Open YourFileX For Output As #iTextFile 'Open to allow us to read.

    Print #iTextFile, "Header" 'write header to New File

    'loop for each line and print to file
    'Repost if you need help with this bit.

    Close #iTextFile

    'next file

    Close #iNewFile


    Read each line into a string and parse inserting commas where required before writing to new file.

  5. #5

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Wink I will go and try this

    Thanks,

    I will go and try this and get back to you

    Cheers

  6. #6
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181

    Question Okay?

    Is the code Steven gave enough? Or do you need some additional explanations?
    Stevens Code is really clear.

    I think you should use read line and print for your outputs but I'm not really shure. Just explore the MSDN
    Sanity is a full time job

    Puh das war harter Stoff!

  7. #7

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Angry Help

    I am getting there, slowly. How do i write data to a new line of a text file. I am able to read in data line by line but when i try and append a new text file with this data, it will not go onto the next line.

    Any ideas

  8. #8

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Unhappy Almost There

    I have sussed the code to take the data from three seperate files and then place it with headers into a single text file.

    How do i prevent this process from happening again. I dont want any duplicate data in my final text file. Once it has been populated once i want to get the system to check how this file is populated. I don't know how big the file will be. If there is any data in the file whatsoever i don't want the process to run again. ( That is any data apart from my headers )

    Any ideas

    Here is my code so far

    Private Sub Command1_Click()

    Dim iTextFile As Integer
    Dim iNewFile As Integer
    Dim data As String
    Dim count As Integer
    Dim i(3) As String
    Dim j(3) As String

    i(1) = "C:\windows\desktop\x.txt"
    i(2) = "C:\windows\desktop\y.txt"
    i(3) = "C:\windows\desktop\z.txt"

    j(1) = "[One]"
    j(2) = "[Two]"
    j(3) = "[Three]"

    iNewFile = FreeFile

    Open "C:\windows\desktop\mark.txt" For Append As #iNewFile

    For count = 1 To 3
    iTextFile = FreeFile
    Open i(count) For Input As #iTextFile
    Print #iNewFile, j(count)
    Do Until EOF(iTextFile)
    Line Input #iTextFile, data
    Print #iNewFile, data
    Loop
    Close #iTextFile
    Next count

    Close #iNewFile

    End Sub


  9. #9
    Guest
    Hey Iceman,

    Dunno if this is useful to you, but I made a DLL recently that changes files from one type to another (delimited or fixed width files) and it allows you to re-arrange the fields in the records.

    email me if you want the URL


  10. #10
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    Do it this way, where:
    FileX is your sample file X
    FileY is your sample file Y
    FileZ is your sample file Z
    MyNewFile is your New generated file


    Drop a button on a form, and make sure that you edit the file names and click the button to do the stuff.


    Code:
    Declaration Section
    Option Explicit
    Dim FileXData As String
    Dim FileYData As String
    Dim FileZData As String
    Dim Buffer As String
    
    
    Sub Form_Load()
    'form load event
    
      'edit these filenames or 
      'create test files in the relavent folder
      FileXData="c:\temp\testfileX.txt"
      FileYData="c:\temp\testfileY.txt"
      FileZData="c:\temp\testfileZ.txt"
      'this is the results file which will be created
      MyNewFile="c:\temp\testresults.txt"
    
    End Sub
    
    
    Sub Command1_Click()
    'command button click event
    
    'init buffers
    Open FileX For Output As #1
    Open FileY For Output As #2
    Open FileZ For Output As #3
    Open MyNewFile For Output As #4
    
    'start with file x
    FileXData=input#(FileLen(FileX),1)
    
    'do file Y Next
    FileYData=input#(FileLen(FileY),2)
    
    'do file Z Next
    FileZData=input#(FileLen(FileZ),3)
    
    '--> parse the file so as to comma delimit it
    ParseMyFiles()
    
    '-->  now your ready to write it out
    'do file x
    Msgbox "Writing File X Header"
    Write #4,"--> File X Header"
    Write #4, FileXData
    Write #4,"--> End File X Header"
    
    'do file y
    Msgbox "Writing File Y Header"
    Write #4,"--> File Y Header"
    Write #4, FileYData
    Write #4,"--> End File Y Header"
    
    'do file z
    Msgbox "Writing File Z Header"
    Write #4,"--> File Z Header"
    Write #4, FileZData
    Write #4,"--> End File Z Header"
    
    'close all buffers
    Close #1
    close #2
    Close #3
    Close #4
    
    End Sub
    
    
    Sub ParseMyFiles()
    'does comma delimiting
    
    'the simplest way (as coding goes) is to search each file
    'for spaces, and this is where the comma will be placed
    '
    'so:- Cats Dogs Mice would become
    '     Cats,Dogs,Mice etc etc
    Dim Counta As Integer
    
      'file x first
      Buffer=""
      For Counta = 1 to Len(FileXData)
        'test for space
        If Mid$(FileXData,Counta,1)=" " Then
          'make change/delimit
          Buffer = Buffer & ","
        Else 
          Buffer = Buffer & Mid$(FileXData,Counta,1)
        End If
      Next
      'update string
      FileXData=Buffer
    
      'file y
      Buffer=""
      For Counta = 1 to Len(FileYData)
        'test for space
        If Mid$(FileYData,Counta,1)=" " Then
          'make change/delimit
          Buffer = Buffer & ","
        Else 
          Buffer = Buffer & Mid$(FileYData,Counta,1)
        End If
      Next
      'update string
      FileYData=Buffer
    
      'file z
      Buffer=""
      For Counta = 1 to Len(FileZData)
        'test for space
        If Mid$(FileZData,Counta,1)=" " Then
          'make change/delimit
          Buffer = Buffer & ","
        Else 
          Buffer = Buffer & Mid$(FileZData,Counta,1)
        End If
      Next
      'update string
      FileZData=Buffer
    
    End Sub
    I know the code could be condensed but have kept it longhand so you can decipher what each line of code does.

    DocZaf
    {;->


  11. #11

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Exclamation Cool

    Thanks, i will have a go at that today.

    Do you have an idea of a solution to my last question about checking if the data has moved correctly into my destination file.

    Regards

    M

  12. #12

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Unhappy Print or Write

    Can Anyone tell me why you would use the WRITE command as apposed to the PRINT command and vise versa

  13. #13
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    iceman, this is copied from MSDN Library:

    Unlike the Print # statement, the Write # statement inserts commas between items and quotation marks around strings as they are written to the file. You don't have to put explicit delimiters in the list. Write # inserts a newline character, that is, a carriage return–linefeed (Chr(13) + Chr(10)), after it has written the final character in outputlist to the file.

    Try them both and see the difference.



  14. #14

    Thread Starter
    Addicted Member icemanmt78's Avatar
    Join Date
    May 2000
    Posts
    142

    Unhappy

    I have been using the Print and the Write but both seem to add a blank line to my final file. How do i get rid of this blank line or stop my code putting it there. Can i use the write statement when i am reading in a string already in quotation marks. The other prob i have is if i use write i get the following in my final text file.

    "[One]"
    """02/11/1999"",""09:30:00"",1,6,89.94,6,2,59.98,2"

    I don't want "" around the header or the date/time or around the whole line

    See my code halfway down if you need to see what i am doing

    Any ideas

  15. #15
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    Try this and see only one method puts quotes around string, as was explained above. Note waht inserts an empt line. Either you are doing this explicitly, or you are reading and writing an empty string.

    Code:
    Sub TestIt4()
        Dim strMine1 As String
        Dim strMine2 As String
        
        strMine1 = "this is test of Write"
        strMine2 = "This is test of Print"
        
        Open "c:\temp.txt" For Output As #1
        Write #1, strMine1
        Write #1,
        Write #1, "this is test of Write line 2"
        Print #1, strMine2
        Print #1,
        Print #1, "This is test of Print line 2"
        
        Close #1
    
    End Sub

  16. #16
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554

    Wink

    I have been using the Print and the Write but both seem to add a blank line to my final file. How do i get rid of this blank line or stop my code putting it there. Can i use the write statement when i am reading in a string already in quotation marks. The other prob i have is if i use write i get the following in my final text file.

    "[One]"
    """02/11/1999"",""09:30:00"",1,6,89.94,6,2,59.98,2"

    I don't want "" around the header or the date/time or around the whole line
    Here is the answer to your second Q:
    Only... theres always a catch... {;->
    The code below will parse your string BUT it will display the answer in a msgbox, but the basic script is correct.
    You can use the script anyhow you like

    NOTE:
    Drop a commandbutton a form and use its default name, paste the code below into the button click event.
    Also add a text box and name that "Sample" and finally
    MAKE SURE TO ENTER YOUR STRING:
    DD/MM/YY,n1,n2.n3.n4

    Thats the date followed by four numbers each seperated by a comma.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    'the date
    Dim tDate As String * 10
    'temporary use
    Dim tword As String
    Dim Counta As Integer
    Dim Countb As Integer
    Dim Msg As String
    Dim gettingsmaller As String
    
       'init counter
       Countb = 0
       'assign data
       gettingsmaller = Sample
       Do
          '-->  check for comma
          'if comma exists
          If InStr(gettingsmaller, ",") <> 0 Then
             'inc counter
             Countb = Countb + 1
             'if date
             If Countb = 0 Then
                'extract date from string
                tDate = Left$(gettingsmaller, InStr(gettingsmaller, ",") - 1)
                'make string smaller
                tword = gettingsmaller
                gettingsmaller = Right(gettingsmaller, Len(gettingsmaller) - InStr(gettingsmaller, ","))
                'strore data to show in a msgbox
                Msg = "Date:- " & tDate & vbCrLf
             'if not date but number
             Else
                'extract & store data to show in a msgbox
                Msg = Msg & "Number" & Countb - 1 & ":- " & Left$(gettingsmaller, InStr(gettingsmaller, ",")) & vbCrLf
                'make string smaller
                tword = gettingsmaller
                gettingsmaller = Right(gettingsmaller, Len(gettingsmaller) - InStr(gettingsmaller, ","))
             End If
          Else
             'if comma not exists
             Exit Do
          End If
       Loop
       
       'get last number
       Msg = Msg & "Number" & Countb + 1 & ":- " & gettingsmaller & vbCrLf
       
       'result
       MsgBox Msg, 64, "Wit Woooooo"
       
    End Sub


    DocZaf
    {;->

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width