LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to insert text data in the middle of template text file at specific locations?

Solved!
Go to solution

Hi all,

I need to add some (text) data to specific locations of an existing text file template and then rename the newly written file.

The way it works, I need to read the contents of 8 different text files (data) and copy and paste those, to 8 specific locations of the template text file. I need to enter the new data from each data text file between the START_TABLE_C1

and END_TABLE_C1 in the template file.

 

Like this ...

 

# Optional section

START_TABLE_C2

 

END_TABLE_C2

 

# Optional section

START_TABLE_C3

 

END_TABLE_C3

 

# Optional section

START_TABLE_C4

 

END_TABLE_C4

 

START_TABLE_C5

 

END_TABLE_C5

 

# Optional section

START_TABLE_C6

 

END_TABLE_C6

 

# Optional section

START_TABLE_C7

 

END_TABLE_C7

 

# Optional section

START_TABLE_C8

 

END_TABLE_C8

 

--------------------------------------------

 

So, the way I approached this is to read the template file and 8 data files into 9 arrays, then I insert each text file data (array)  to specific location of the template array by it’s index number.

But this is not good enough because the data text data files can vary from file to file and I don’t always have the same number of lines in the (data) text files. 

Aditionally, I have file path controls (9 of them) to find the text files and write to the template.

I like to be able to just enter the serial number of the file, create the path to the data file and find it in the folder. 

Appreciate your help, Thanks.

 
Download All
0 Kudos
Message 1 of 11
(5,576 Views)
Use Set File Position Function
it is in advance file function in File I/O
0 Kudos
Message 2 of 11
(5,557 Views)

are you sure that will work?

 

as I said, I want to insert/write in between the START and END tags

Set File Position Function only gives me start, end and current positions. 

 

0 Kudos
Message 3 of 11
(5,543 Views)

I would use Read Text File.vi instead of Read Spreadsheet File.vi Right click the function and select "read lines" and then set the count input to -1. The output will be a 1D arrary of the file lines. Then use Search 1D Array to get the index of START_TABLE_Cx (add 1 to the index to add after. Finally use Write To Text File to write to the new file.

 

Ben64

 

Insert in Text File.png

0 Kudos
Message 4 of 11
(5,519 Views)

Sorry i got what is your means now
I think ben give the method to do
also this method maybe work
just before start select your 
main file= temp
your first data 
and place and name for new file

0 Kudos
Message 5 of 11
(5,501 Views)

Hi,

 

Ben, Thanks, it works for one data file insertion into template but I tried doeing it for multiple data file insertion and didint work for me, it overwrites the sections.

I am attching the rsult file also

 

Hatef, Thanks, I keep getting duplicate file path  error running the vi, 

 

 

Download All
0 Kudos
Message 6 of 11
(5,480 Views)

alex75 a écrit :

Hi,

 

Ben, Thanks, it works for one data file insertion into template but I tried doeing it for multiple data file insertion and didint work for me, it overwrites the sections.

I am attching the rsult file also

 

Hatef, Thanks, I keep getting duplicate file path  error running the vi, 

 

 


You need to do the new insertion in the previously modified array.

 

Ben64

 

curve.png

Message 7 of 11
(5,465 Views)

OK I just make a little change to fix that problem to
the method is same 
if you find more problem So just try fix problems to use

there are not serious problem 

0 Kudos
Message 8 of 11
(5,454 Views)
Solution
Accepted by alex75

Here is a more compact and efficient way to do it. You could eventually replace the path array input section with the Recursive File List VI. (from the  File I/O - Advanced File Functions palette, have a look at the help file, you can search for a pattern in the filename and it returns an array of the file found paths).

 

Ben64

 

curve_simplified.png

Message 9 of 11
(5,432 Views)

Hi Ben, Thanks, it works fine.

I also modified/added to it,  so that instead of file path , serial number of data files will be entered/read from operators input , and data file will be found in the folder to be read and pasted to the template.

now I need some safeguard that if the file is not found in the folder (like if operator enters a wrong serial number) what the program will decide, retry searching for file, continue , or stop?

also I need to use an event structure to start the test when all the string controls are inputted.

Thanks

 

 

 

Hi Hatef... I got the program running without an error, but it doesn't wite to the new file yet. anyways I got the answer for the first problem I had, thanks again.

0 Kudos
Message 10 of 11
(5,374 Views)