Results 1 to 5 of 5

Identifying a text file as UNIX or DOS format

Threaded View

  1. #1
    Registered User
    Join Date
    04-14-2011
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003
    Posts
    5

    Identifying a text file as UNIX or DOS format

    Hello,
    I would be very grateful for some help from the VBA I/O-experienced on this forum.

    I'm writing an application to do some conversion on log file timestamps. I have it mostly completed, but am stuck on one issue: These files are text files which could have come from Windows/DOS or UNIX.
    I am reading them using sequential access (Open filename for Input as #n, etc)

    If they are Windows, it is pretty straightforward:
    • text file lines are terminated by CR/LF (or vbCrlf) and Excel reads them fine, splitting up neatly into records
    If they are from UNIX, then I have a problem.
    • The records are terminated just with an LF which VBA does not recognize as a terminator, So it reads the whole file in as one humongous record.

    Now this is okay. In the UNIX streams I can replace the LFs with CR/LFs and write out the file using binary I/O. It works.

    Heres my problem: I don't know in advance whether a file is going to be UNIX or DOS format. And what I'm looking for is the most neat/effective way to determine this.
    I can do something like count the records, and if there are more than one, conclude that it is a DOS file. But that seems clumsy, and these files are pretty big. The first one I tested was UNIX and gave me one string of length 800,000+ .

    Any suggestiobns/solutions?
    Cheers and many thanks in advance


    For completeness, here's my code for the UnIX to DOS conversion:

    Public Function CnvFil(InputFile As String, OutputFile As String)    
    
        Dim Ifn as integer, Ofn as Integer
        Dim Buffer As String
        
        Ifn = FreeFile
        Open InputFile For Input As #Ifn 
        Ofn = FreeFile 
        Open OutputFile For Binary As #Ofn
        Do While Not EOF(Ifn)
                    Line Input #Ifn, Buffer  'if UNIX then there will be only one line
                    Buffer = Replace(Buffer, vbLf, vbCrLf)
                    Put #Ofn , Buffer  ' write file
         Loop
         Close #Ofn
         Close #Ifn
         
     End Function
    Last edited by radiocam; 04-23-2011 at 11:43 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1