Results 1 to 1 of 1
  1. #1
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694

    Data / File Streaming

    File manipulation can be done very easily through VB code. The easiest way, IMO, is to use a simple I/O (input/output) method rather than to mess around with the File System Object ("fso" variable you usually see in code). I/O is popular when working with binary files, etc. The I/O method is nothing more than a technique that's available in VB where you can open a sort of "portal" and throw data back and forth between two files.

    But at any rate...there's plenty of stuff that I/O can do for you, and even simplify. Namely, it can perform plenty of one-liner operations that would otherwise take some thought. Some are:

    Get a file size in bytes:
    Code:
    File Size = FileLen("Path of File")
    and of course from there you can use simple multiplication to calculate the format (KB, MB, GB, whatever) of the file size that you want.


    Copy a File:
    Code:
    FileCopy "Source File", "New File"
    Rename a File or Directory:
    Code:
    Name "OldPath" As "NewPath"
    Delete Files:
    Code:
    Kill "FileName"
    Kill "[Directory]\*.[Extension]" 'To delete all files of one specific type
    Get Last Modification Timestamp for a File:
    Code:
    FileDateTime "FilePath"
    But not only are the above operations available, you can use I/O to simplify data sharing in files like .txt, .ascii, etc...

    To read a file, open it for Input:
    Code:
    Open "FilePath" For Input As #1
    Once your file is open...

    Get the entire file contents:
    Code:
    Variable = Input(LOF(1), 1)
    (what's amusing about the above method is that LOF() works because INPUT() asks for the number of characters to read and LOF() returns the size of the file in bytes. So this one liner works great because 1 binary byte = 1 ASCII character).


    To write to a file, open it for Output:
    Code:
    Open "FilePath" For Output As #1
    Once your file is open...

    Write Data To The File:
    Code:
    Write #1, "String"
    Append Data To The End of the File (requires different I/O mode):
    Code:
    Open "FilePath" For Append As #1
    Write #1, "String"

    So that's a little of the easy stuff. You can do all sorts of other things with I/O that don't take near as much time as other methods in VBA do. Most of them you can find in VBA help. The Get, Put, EOF and LOF can also be useful, but they require a little more study to get use to.
    Last edited by ajetrumpet; 06-24-2011 at 06:30 PM.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing new data from same file once a week
    By fclanton in forum Import/Export Data
    Replies: 0
    Last Post: 11-11-2010, 09:22 AM
  2. importing data from csv file
    By sbglobal in forum Import/Export Data
    Replies: 2
    Last Post: 08-22-2010, 11:45 AM
  3. Export data to .doc(x) file
    By fat drummer in forum Import/Export Data
    Replies: 0
    Last Post: 07-28-2010, 03:51 PM
  4. Data from text file
    By Directlinq in forum Programming
    Replies: 1
    Last Post: 10-19-2009, 02:29 AM
  5. Exporting data to text file
    By NC_juggler in forum Import/Export Data
    Replies: 0
    Last Post: 11-21-2008, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums