LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to split one Text file into multiple text files ??

 

Hello NI Developers!

 

 I have a problem. At first I thought that this will be very easy, but It's not, maybe only for me.

 

I have a text file and there are many articles inside. Those articles are split to each other by several empty lines.

There are 3-5 lines between articles, but there are also empty lines in each article but not more then 2 empty lines.

 

I need a VI that could read all text from file, then count empty lines and if it finds 3-5 empty lines - split each article into 

separate file.

 

For example if there is text inside file like this:

 

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

TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 

TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 

_                                                                         (1 empty line)

TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 

TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 

TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 TEXT-1 

_

_

_                                                                         Empty lines (3 - 5)

_

TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 

_                                                                         (1 empty line)

TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 

TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 TEXT-2 

_

_

_

 

TEXT-3 TEXT-3 TEXT-3 TEXT-3 TEXT-3 TEXT-3 

TEXT-3 TEXT-3 TEXT-3 TEXT-3 TEXT-3 TEXT-3   

 ......

 ......

 

 

 I need to place this TEXT1, TEXT2, TEXT3 into individual files.

 

 I tried for loop, while loop, but no success... Simply not working, it writes all text into one file.

 

Any help much appreciated, 

 Thanks in advance

 

 

 

 

Download All
0 Kudos
Message 1 of 9
(7,228 Views)

You basically have three fundamental flaws with your program:

 

  • A blank line isn't really a blank line with text files. It still contains an EOL, and on Windows this is a CR/LF (2 characters). Thus your comparison for the string length to be 0 will never work.
  • You are never changing the name of the output file, so you will only ever get one output file.
  • You are rebuilding the text file inside the loop with the Insert Array function. This will guarantee that the last write operation simply writes out the original array.


Off the top of my head, I can think of two basic ways of solving the problem:

 

  • Read the whole text file and then in a loop (you can use a for-loop since you know the number of lines) you would conditionally build up an array if you have not encountered a "3 empty lines" condition. Once you've met that condition, write out the array of lines you've accumulated so far, clear out the array, and change the filename to the next one (e.g., "output2.txt").
  • Read the whole text file, and then scan the array for the number of breaks that would indicate how many new files you would need. Record the indices where the files would start and stop.  Then, copy the original file N times, where N is the count you found out earlier for how many files you would need. Then, using the start/stop indices you can calculate the required lengths of the files and set the EOF for each file.

I'd go for solution 1 since it's relatively easy. I'd write the code for you, but where's the fun in that for you? Smiley Wink
Message 2 of 9
(7,213 Views)

 

 

 

1) I tried making this code and spent about 12 hours.

 

 

2) Believe me, I tried different file names, but only in this example I removed part of code, because it makes me mad already.

 

 

3) Your suggested comparison with [LR/CR] to fetch an empty line is not working

 

4) What do you suggest using instead of function "Insert into Array" ? 

0 Kudos
Message 3 of 9
(7,187 Views)

 

This is not working. Why ??EmptyLine.JPG 

 

 

 

 

0 Kudos
Message 4 of 9
(7,184 Views)

Sorry, my mistake. The EOL will get chopped off. Everything else I said was correct, though. Note that you could have easily determined this by putting a probe on the string wire.

 

See if the attached works for you. I didn't test it all that much, so you may need to correct it. Note that it's in 8.2. For 8.5 or higher you can make the for-loop a conditional stop on error so you don't keep spinning if you get a file I/O error. 

Message 5 of 9
(7,171 Views)

 

 

Thank you smercurio_fc for help.

 

Comaprison to CR/LF works just perfect. It was my mistake as I did it not correct.

 

I've just finished my implementation and saw your reply. See my attached VI, it's not so pretty like your, but it is working. Anyway I guess I should learn some tips/tricks from your coding style. I like code where is used Ternary operator (Select node), it looks great, but I tried doing myself this way and couldn't get code to work and was confused. 

 

 Thank you!

 

0 Kudos
Message 6 of 9
(7,160 Views)

smercurio_fc wrote: " For 8.5 or higher you can make the for-loop a conditional stop on error so you don't keep spinning if you get a file I/O error. "

 

Actually I think it can be done in any version of LabView, isn't it ?

 

 Simply unbundle True/False data from Error cluster and wire it to Case structure.

0 Kudos
Message 7 of 9
(7,159 Views)
Just forgot to add. To get this VI work - you need change that path inside from " C:\ " to exinsting directory like " C:\test\ "
0 Kudos
Message 8 of 9
(7,156 Views)

ACiDuser wrote:

smercurio_fc wrote: " For 8.5 or higher you can make the for-loop a conditional stop on error so you don't keep spinning if you get a file I/O error. "

 

Actually I think it can be done in any version of LabView, isn't it ?

 

 Simply unbundle True/False data from Error cluster and wire it to Case structure.


No, I'm talking about the for-loop in LabVIEW 8.5 or higher having a conditional terminal just like a while loop.

Message 9 of 9
(7,135 Views)