Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Read text file and UNIX end of line

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
swellfr@googlemail.com - 12 Dec 2007 18:58 GMT
Hi,
   I would like to know if there is a clean way to read files created
under unix ( so terminating by LF instead if CRLF on Windows) in VBA.
It seems that when i call "Line Input" , i get the full file at once .
Would it be possible to get it line by line?

Thx
Joel - 12 Dec 2007 20:05 GMT
Yes you can.  The code below converts unix to dos fixing the Cr and Linefeed
so they are dos format.

Unix files only need a carriage return, dos uses both Carriagge return and
linefeed.

If you just want to modify the code below as required.  To read the UNIX
file you need to open the file in binary mode andread one character at a
time.  the end of the line would be when yo uget to the CR.

Sub fixcrlf()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   
Dim CR As String
Dim LF As String

CR = Chr(13)
LF = Chr(10)

  Set fsread = CreateObject("Scripting.FileSystemObject")
  Set fswrite = CreateObject("Scripting.FileSystemObject")
   
  ReadFileName = "intext.txt"
  WriteFileName = "outtext.txt"

  'open files
  ReadPathName = MyPath + ReadFileName
  Set fread = fsread.GetFile(ReadPathName)
  Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

  WritePathName = MyPath + WriteFileName
  fswrite.CreateTextFile WritePathName
  Set fwrite = fswrite.GetFile(WritePathName)
  Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

  OutputLine = ""
  FoundCR = False
  Do While tsread.atendofstream = False
     
     MyChar = tsread.Read(1)

     Select Case MyChar
     
     Case LF
        If FoundCR = False Then
           tswrite.write CR
        Else
           FoundCR = False
        End If
     Case CR
        If FoundCR = True Then
           tswrite.write LF
        Else
           FoundCR = True
        End If
     Case Else
        If FoundCR = True Then
           tswrite.write LF
           FoundCR = False
        End If
     End Select
           tswrite.write MyChar
  Loop
             
  tswrite.Close
  tsread.Close
 
End Sub

> Hi,
>     I would like to know if there is a clean way to read files created
[quoted text clipped - 3 lines]
>
> Thx
Joel - 12 Dec 2007 20:05 GMT
Yes you can.  The code below converts unix to dos fixing the Cr and Linefeed
so they are dos format.

Unix files only need a carriage return, dos uses both Carriagge return and
linefeed.

If you just want to modify the code below as required.  To read the UNIX
file you need to open the file in binary mode andread one character at a
time.  the end of the line would be when yo uget to the CR.

Sub fixcrlf()

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   
Dim CR As String
Dim LF As String

CR = Chr(13)
LF = Chr(10)

  Set fsread = CreateObject("Scripting.FileSystemObject")
  Set fswrite = CreateObject("Scripting.FileSystemObject")
   
  ReadFileName = "intext.txt"
  WriteFileName = "outtext.txt"

  'open files
  ReadPathName = MyPath + ReadFileName
  Set fread = fsread.GetFile(ReadPathName)
  Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault)

  WritePathName = MyPath + WriteFileName
  fswrite.CreateTextFile WritePathName
  Set fwrite = fswrite.GetFile(WritePathName)
  Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

  OutputLine = ""
  FoundCR = False
  Do While tsread.atendofstream = False
     
     MyChar = tsread.Read(1)

     Select Case MyChar
     
     Case LF
        If FoundCR = False Then
           tswrite.write CR
        Else
           FoundCR = False
        End If
     Case CR
        If FoundCR = True Then
           tswrite.write LF
        Else
           FoundCR = True
        End If
     Case Else
        If FoundCR = True Then
           tswrite.write LF
           FoundCR = False
        End If
     End Select
           tswrite.write MyChar
  Loop
             
  tswrite.Close
  tsread.Close
 
End Sub

> Hi,
>     I would like to know if there is a clean way to read files created
[quoted text clipped - 3 lines]
>
> Thx
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.