+ Reply to Thread
Results 1 to 5 of 5

Identifying a text file as UNIX or DOS format

  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:

    Please Login or Register  to view this content.
    Last edited by radiocam; 04-23-2011 at 11:43 AM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Identifying a text file as UNIX or DOS format

    I don't think functions are made for this.
    Use a normal macro with arguments instead.

    Please Login or Register  to view this content.
    Last edited by snb; 04-22-2011 at 03:08 PM.



  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,528

    Re: Identifying a text file as UNIX or DOS format

    What would be the typical length of a record in DOS and what would be the typical length of a record in UNIX? I'm assuming DOS would be relatively short and UNIX would be relatively long. So, read a record, check how long the record is and, if it's greater than a predetermined length, it's UNIX.

    Just a thought.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


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

    Re: Identifying a text file as UNIX or DOS format

    Thanks guys.

    @snb: A real elegant approach. (I had a function rather than a sub more by accident than intent.)

    I have to tweak your replace strings slightly: The unix files just have an LF, the DOS files have a CRLF. With your suggested replacements I'd wind up with just single CRs.

    This string would give me CRLFs:
    Please Login or Register  to view this content.

    Your approach is real neat. I now need to experiment with a few of the larger files to check performance, etc - but thanks. Nice resolution!

    Cheers

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Identifying a text file as UNIX or DOS format

    I can't remember the sequence vbcr & vblf or vblf & vbcr
    Please Login or Register  to view this content.

+ Reply to Thread

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