CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Read, open and extract data from excel .csv file

  1. #1
    Join Date
    Sep 2005

    Lightbulb Read, open and extract data from excel .csv file


    I am new to this forum and would really appreaciate some assistance with what I'm trying to do.

    I am trying to create a VB 6.0 program that automatically reads a .csv file created in a specific directory (say c:\), open it and then copy a new entry in a new row.

    More detail:

    There is a new excel .csv file created each day (generated by a test procedure) that contains test results. Each day a test is performed a new file is created...the first test will take on the first row in the excel workbook. Each consecutive test afterwards takes on the second and the third and so on. The program that does this is some alien program. [New file for each test day and new row for each single test]. No test in a day, no file created.

    ?Is it possible to create a program that automatically reads a new file created in a directory as well as any new entry in that file (excel file) and then make a copy of it (...then paste it to another excel file)?

    Your help will be really appreciated...thanks

  2. #2
    Join Date
    Jan 2004
    San Diego

    Re: Read, open and extract data from excel .csv file

    A raw .csv file can be treated just like a text file. So you can literally just do standard text file I/O operations to append the master file. I'm sure there may be more elegant or efficient ways of doing this but this is how I have always done it and never had problems with unusual/unreliable results or errors.
    ' master.csv is main file for all tests
    ' x.csv is file with tests for current day
    strInFile = "c:\master.csv"
    strReadFile = "c:\x.csv"
    ' Open main file a set for appending
    Set InFile = FileInput.GetFile(strInFile)
    Set AppStream = InFile.OpenAsTextStream(ForAppending, TristateFalse)
    ' Open test file for reading
    Set OutFile = FileOutput.GetFile(strReadFile)
    Set ReadStream = OutFile.OpenAsTextStream(ForReading, TristateFalse)
    Do While ReadStream.AtEndOfStream <> True
      strReadLine = ReadStream.ReadLine
    Set FileOutput = Nothing
    Set OutFile = Nothing
    Set FileInput = Nothing
    Set InFile = Nothing
    FileInput and FileOutput are FileSystemObject objects, OutFile and Infile are File objects and AppStream and ReadStream are TextStream objects.

    You will need to modify this code to suit your needs such as set the readfile to be in the nomenclature for the save files from your testing program. I assume it names the file in some manner based off of the current date. So you could even design your program to do a FindFile operation (instead of GetFile for setting OutFile) to see if there is a file for that day's tests and if there is it will do the appending. Utilize the VB help files to read more on the FileSystemObject and its related objects and methods; that should give you any additional information you may need and you can then always ask us for further assistance if you don't understand something in there.
    Death is life's special way of telling you you're fired.

    For I do not seek to understand in order to believe, but I believe in order to understand. For I believe this: unless I believe, I will not understand. - Anselm of Canterbury (10331109)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)