Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2007, 10:28 AM
Registered User
 
Join Date: Oct 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input with Unix files

I am trying to write a script to read lines from a file that was created by a unix-based system. There are too many lines in the file to just open it as fixed-width text. (I'm reading each line, checking for certain values, and maybe writing it to the worksheet) I've worked with files that use <cr>(ascii 13) at the end of lines, and files that use <cr><lf>(ascii 13/10) at the end of lines, no problem, but this one just uses the <lf>(ascii 10).

Using

Input #1 LineString

attempts to read the whole file into a string variable, instead of just a single line of text.

If I open the file into Wordpad and re-save it as text, Input #1 works fine. I'd like to avoid this step if possible, because some of these files are large, and I'd like to avoid reading, writing, and re-reading them.

Is there any way to treat the <lf> alone as an end-of-line character?
It's kind of strange that it isn't automatically read that way.

Oh, using Excel 2003 (11.8146.8132) SP2, in case that matters.
 
Old October 16th, 2007, 04:52 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Get 1 character at a time. It's a little slower but you won't have to modify your files:
-------------------------------------------------------------------
Private Sub CommandButton1_Click()

  Dim sChar As String, sLine As String
  Open "C:\Test.txt" For Input As #1
  Do While Not EOF(1)
    sChar = Input(1, #1) 'Retrieve 1 character.
    If sChar = Chr(13) Then
      MsgBox sLine
      sLine = ""
    Else
      sLine = sLine & sChar
    End If
  Loop
  Close #1
  If sLine <> "" Then MsgBox sLine 'shows last line when necessary.

End Sub
-------------------------------------------------------------------

Hope this helps.

 
Old October 16th, 2007, 04:54 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

P.S.
Remember to change Chr(13) with Chr(10) in your case.

 
Old October 17th, 2007, 11:05 AM
Registered User
 
Join Date: Oct 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! That is quite a bit slower with a really big file, but I can leave it run while I'm doing other things, and it's still faster than loading, saving, and reloading the file. It also uses a lot less disk space.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Input files? edubble XSLT 5 July 31st, 2007 03:42 AM
Reading input from files PROLOG Ruman29 Other Programming Languages 0 March 8th, 2007 12:49 PM
-s command in Unix crmpicco Linux 4 November 28th, 2006 12:10 PM
about "UNIX" Thierrypp2006 BOOK: Beginning Unix ISBN: 0-7645-7994-0 2 October 9th, 2006 11:23 PM
UNIX Timestamp apek PHP How-To 4 January 20th, 2004 02:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.