Read / Write To Text Files (1 Viewer)

Status
Not open for further replies.

ajetrumpet

Banned
Local time
Today, 07:53
Joined
Jun 22, 2007
Messages
5,638
The attached example shows how you can get data into and out of a text file by using the File System Object and variations of the Read/Write methods.

This is probably the most common method used, as it writes in append mode, and not in write mode, and it also reads an entire file instead of only the first line...


3/6/2010
I will post some streaming examples once in a while here. Most of it will be code that I use or have used.

Code to create a (comma) delimited text file spreadsheet of all files in a windows folder:
PHP:
    Dim fs, f, f1, fc, s, oFile, folderspec
    folderspec = "FOLDER ADDRESS"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 In fc

s = s & Left(f1.Name, (Len(f1.Name) - 4)) & "," & Round(f1.Size / 1048576, 0) & "mb"
s = s & vbCrLf

    Next
       
   Set oFile = fs.createtextfile("COMPLETE ADDRESS OF NEW TEXT FILE", True)

   oFile.writeline s
   oFile.Close
   
    Set f = Nothing
    Set fc = Nothing
    Set fs = Nothing
    Set oFile = Nothing


I just ran into a text file that had no definite delimiter and it wasn't fixed width either (don't ask me where I got it). Interesting format:



My fix-it solution to make it importable into Access:
PHP:
    Dim fs, s, oFile, strpart, strsplit, retstring
    Set fs = CreateObject("Scripting.FileSystemObject")
       
   Set oFile = fs.opentextfile("FILE'S PATH", 1, False)

   s = oFile.read(111111111)
   oFile.Close
   
    For Each strpart In Split(s, vbCrLf)
        If InStr(strpart, "-----") = 0 Then
            For Each strsplit In Split(strpart, "|")
                retstring = retstring & Trim(strsplit) & ","
            Next strsplit
                    retstring = Trim( _
                                Right( _
                                Replace( _
                                    retstring, ",,", ""), _
                                    (Len(retstring) - 1))) & vbCrLf
                                
        End If
    Next strpart
    
   Set oFile = fs.createtextfile("FILE'S PATH", True)

   oFile.writeline retstring
   oFile.Close
   
    Set fs = Nothing
    Set oFile = Nothing
 

Attachments

  • Text File Streaming.zip
    22.3 KB · Views: 791
  • Text File Streaming (2000).zip
    22.2 KB · Views: 711
Last edited:

JohnLee

Registered User.
Local time
Today, 05:53
Joined
Mar 8, 2007
Messages
692
Hi ajetrumpet,

I'm looking to learn about how to write code that reads and writes to text files, I came across your thread and downloaded your zip file, unfortunately I am using a lower version and therefore have been unable to view the contents of your database.

I thought I'd ask, would it be possible for you to provided a way of accessing the information in your database so that I can learn how to read and write to text files.

I'm using Access 2000.

your assistance would be appreciated.

John Lee
 

ajetrumpet

Banned
Local time
Today, 07:53
Joined
Jun 22, 2007
Messages
5,638
John,

I posted a 2000 version. Good luck.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom