DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

read from then write to text file

I am working on an application in which I import a large amount of data, process it, show report pages and then create a file structure with some of the processed data for use in a vehicle simulation model.  The file structure is fixed by the simulation program and contains a main folder with assorted sub folders.  Since the structure is predefined I have a "template" directory that I pull the skeleton information from.  The files contained in the template are text files with comments and definition of variables that are used for reference by the modeler later.  Also in the file is the string "PATH_START".  This is the marker for me to begin inserting the calculated information.
 
I can handle the copy of the structure and renaming files, but I can't seem to open the template file for read/write access and find the string "PATH_START".  If I open the file for just read I have no problem, but I also want to then start writing at the line that contains "PATH_START".
 
It would be a real pain to have to open a source file for read then copy line by line into a new file opened for write.  Is there any other way?
 
Attached is a sample of a template file.
 
Thanks,
 
Wayne 
0 Kudos
Message 1 of 4
(4,877 Views)

Sorry about the duplicate posts I wanted to add some info to the original message, but the board would not let me.  Here is a better description of the problem.

I am working on an application in which I import a large amount of data, process it, show report pages and then create a file structure with some of the processed data for use in a vehicle simulation model.  The file structure is fixed by the simulation program and contains a main folder with assorted sub folders.  Since the structure is predefined I have a "template" directory that I pull the skeleton information from.  The files contained in the template are text files with comments and definition of variables that are used for reference by the modeler later.  Also in the file is the string "PATH_START".  This is the marker for me to begin inserting the calculated information.  There will always be text before the marker, and sometimes text after the marker.
 
I can handle the copy of the structure and renaming files, but I can't seem to open the template file for read/write access and then find the string "PATH_START".  
 
I open the file using myHandle = TextFileOpen("MyFileName.txt", TfWrite). 
I then use the following line to read the file str = TextFileReadLn(myHandle).  After the command str = "". 
 
If I open the file for just read myHandle = TextFileOpen("MyFileName.txt", TfRead) I have no problem, but I also want to start writing when I find the marker which you can't do with a TfRead setting..
 
It would be a real pain to have to open a source file for read then copy line by line into a new file opened for write.  Is there any other way?
 
Attached is a sample of a template file.
 
Thanks,
 
Wayne 
0 Kudos
Message 2 of 4
(4,875 Views)
Hello Wayne!
 
The TextFileOpen command will not accept a combination of read and write access. Even if it was able to do so, it only will be able to append lines and not to insert somewhere in the file. From my point of view the easiest solution is to read the whole file into a string, replace the 'PATH_START' and save the string. For some reason I never used the inbuild DIAdem commands for this. I prefer to use the Windows FileSystemObject. This allows to read or write a complete file from a string with one command.
 
Have a look at this code (no error handling included!):
Sub FileReplace(ByRef sgFilename, ByRef sgPathStart)
  Dim oFile
  Dim sgContent
 
  ' Read
  Set oFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(sgFilename,,,-2)
  sgContent = oFile.ReadAll()
  Call oFile.Close()
 
  ' Replace
  sgContent = Replace(sgContent,"PATH_START",sgPathStart)
 
  ' Write
  Set oFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(sgFilename,true)
  Call oFile.Write(sgContent)
  Call oFile.Close()
End Sub
Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 3 of 4
(4,872 Views)

Thanks for the help.  I have not used the FSO methods much, so I will have to exlpore what other activities would be suited for that method.

Again Thanks,

Wayne

0 Kudos
Message 4 of 4
(4,847 Views)