VBA Excel Reading text file from certain line number onward - vba

say we have an external program, that writes a text file during a long calculation.
Now, I need to read that file in order to check the status of calculation (residuals values, stop on error etc).
The file has size of up to 1Mb. Since the program only adds new information to the end of the file, I dont want to reopen it and reread from the very beginning each time.
Is there a way to start reading the file from a specific line number (where I stopped last time)?
What happens to the file when it is opened in Excel for reading, and after that it is changed by the external program? EoF position shifts forward?

You can use the statement Seek [#]filenumber, position to move to a specific byte in a opened file. Just save the position in the file where you stopped with the function Seek( filenumber ) or with LOF( filennumber ) if you reached the EOF( filenumber ).

Related

Same file path and files in LabVIEW

I saved the data in txt file. Everything is ok. Now, I want to show the data on the chart then that data send by email.
First, I select the txt file from the dialog and then show it on the chart or graph which is ok till here. Then, I want to attach that file automatically and after 5 sec sends it to a specific email. my problem is that I don't want to create a control for attach to choose the txt file, I want to select once the file , then read, then that file email.
Normally, I must select once to read and another time again to select that file (2 times) but I want to select one time the txt file then read and after 5 sec that file attaches and email. I cannot connect attach file wire (RED Wire) to the file dialog because the type of attach is 1D array file path and the type of file dialog is path, I don't know what should I do.
Thanks for helping me.
Use Build Array function to convert your single path (which goes from path select dialog) to 1D array of paths.

Vb.net What is the best way to parse a txt file that gets updated

what I need to accomplish: I need to read a text file that gets updated every 10 sec and dump the new data into. I need to find the best way to do this. I've tried streamreader and file.readalllines, I can get the contents of the txt file, but I don't know how to compare and dump the added lines. Any input would be appreciated, thank you.
Under the assumptions that this is a log file you're reading, where the data is being appended to end, and you're reading the file every 10 seconds, why don't you keep track of the number of lines read or the last line number from the last read?
You can then check to see whether the current read has exceeded the previous line number or number of lines read. If it has exceeded, you should be able to work out what the new lines were added because you know where you read until previously. You can read from the (previous read line + 1) until the end of file.

assignging text file contents to variable

Stack!
I'm currently trying to code a neat little function in Visual Basic that deletes my entire file system for my program depending on the contents of one text file.
If my text file contains the number 0 by itself, my file system will be deleted. If it contains a 1 by itself, it will not delete the file system and carry on with its pathway of execution.
the problem I'm having is reading the file, then assigning the text file's contents to a variable (string), which then is used in an if statement to decide whether it deletes (then immediately recreates) the file system, or keeps it. Ideally I'd be using this primarily for when the user installs. By default, it is set to 0 so that it clears any stored content, then after creating my directory, the number turns to 1 so that it does not reset my programs content directory.
Here's my code. The first line is giving me difficulties -
Could not find file 'C:\Users\110fa_000\documents\visual studio 2013\Projects\Novation_Launchpad_Emulator\Novation_Launchpad_Emulator\bin\Debug\0'.
fileReader = File.ReadAllText(My.Resources.FirstTimeUse)
If fileReader = "0" Then
FirstTimeUse = True
End If
If you can help me, that'd be absolutely brilliant. I don't think there's any more information I can give sadly, but everything regarding the function, purpose, and error is there.

vb.net writing files of specific size

I am creating an application whereby I need to write files (many, many of them). However, I don't want to write files that are too big because the program that consumes them will crash :)
Essentially, I would like to create files that are no larger than a specific size (possibly 5MB). My data is of CSV format and I will be writing line by line or into some stringbuilder to create the file I need.
Question: Is there a way to write data and create a set of files that are no larger than a set size?
I assume you are creating CSV rows 1 at a time.
If you are using a StreamWriter on the file (hopefully you are, no need to hold it all in memory):
Create the new record (a string of CSV items).
Look at the StreamWriter's BaseStream.Length property (Caveat - You need to use AutoFlush to make that property correct with the data that's been written previously).
Add your stream position, new record's length, and your line separator's length (usually 1 or 2, depending on Cr, Lf, or CrLf), and see if it exceeds your threshold.
If no, write the line feed, the record, and continue. If yes, close your StreamWriter and open a new one for a new file, write the record, and continue.

vb.NET SaveAs not saving all Excel data

I have a very strange issue that I cannot seem to find an answer to online.
I have a VB.NET application that creates an Excel of data (roughly 42,542 rows in total) and the saves the file to a folder location & opens it on screen for the user.
The onscreen version & folder version is only showing 16,372 rows of data like it is being cut off.
When I go through debug I can see all the rows are being added & if I save manually in debug all the rows save. Some data seems to get lost on the system save.
I am taking data from 4 record sets & writing each set one after the other with specific headers for each block on the Excel sheet.
My save line is:
xlWBook.SaveAs(Filename:=sFileName, FileFormat:=Excel.XlFileFormat.xlExcel7)
Would anyone please have any ideas as to what this might be?
Older version of Excel only support 16,384 rows per worksheet. You are saving as Excel7 (which is Excel 95) and has this limitation:
See here for a summary of sizes per version:
https://superuser.com/questions/366468/what-is-the-maximum-allowed-rows-in-a-microsoft-excel-xls-or-xlsx
Change your code to another format, See here for all the allowed formats: XlFileFormat Enumeration
However the file format is actually an optional argument in the SaveAs method, so you could leave it off altogether: "For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used."
Source: WorkBook.SaveAs Method