Need help breaking text file with multi lines into individual one line text files

Post Reply
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Need help breaking text file with multi lines into individual one line text files

Post by Zoranj »

I hope I am explaining it right.
I have a single text file with 50 lines of text

Code: Select all

line1
line2
line3
...
I want to get 50 text files with one line of text:

Code: Select all

line1.txt
line2.txt
line3.txt
...
Gabriel had Javascript that does similar thing but it logs individual lines only, does not output them.
Is it possible to modify it to output text files instead?

Code: Select all

// Helper function
	var forEach = function(array, callback){
   var currentValue, index;
   for (i = 0; i < array.length; i += 1) {
      if(typeof array[i] == "undefined"){
         currentValue = null;
      } else {   
         currentValue = array[i];
      }
      index = i;
      callback(currentValue, i, array);
    }
}
	// Construct File object
	var file = new File( job.getPath() );
	
	/// Open file as read only
	file.open( File.ReadOnly );
	
	// Create an array of the lines of the file
	var file_as_array = file.readLines();
	
	// Close the file
	file.close();
	
	
	// Get the first line
	// var first_line = file_as_array[0]; // This is how you get the first line if you need it
	
	// Slice the array without the first line
	var sliced_array = file_as_array.slice(1, (file_as_array.length-1));
	
	// Below this line is all psuedo code
	var new_file_string;
	forEach(sliced_array, function(line, index){
		new_file_string += line + "\n"; // Add each line to the string, one by one
	});
	
	var new_path = job.createPathWithName("whatever.txt");
	
	File.write(new_path, new_file_string, "UTF-8");
	
	// Send job to next flow element
	job.sendToSingle( new_path  );
mkayyyy
Member
Posts: 78
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Need help breaking text file with multi lines into individual one line text files

Post by mkayyyy »

Just replied open-automation/Lobby but thought I'd put it here as well. This script will output the text files as you're wanting:

Code: Select all

function jobArrived( s : Switch, job : Job ) {
    // Construct file object from path of incoming job
    var inFile = new File(job.getPath());

    // Open the file and read contents into array
    inFile.open(File.ReadOnly);
    var lines = inFile.readLines();
    inFile.close();

    for (var i = 0; i < lines.length; i += 1) {
        // Create temp path 
        var lineFileName = job.getNameProper() + "_" + (i + 1 < 10 ? "0" + (i + 1) : i + 1) + ".txt";
        var lineFile = job.createPathWithName(lineFileName);

        // Write contents of current line to temporary file
        File.write(lineFile, lines[i], "UTF-8");

        // Send temporary file to outgoing connection
        job.sendToSingle(lineFile);
    }    

    // Send incoming job to null
    job.sendToNull(job.getPath());
}
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need help breaking text file with multi lines into individual one line text files

Post by Zoranj »

Awesome help Mathew, thank you very much.

Not to be too greedy, but can you edit add function to remove blank lines as well? :oops:
mkayyyy
Member
Posts: 78
Joined: Mon Nov 21, 2016 6:31 pm
Location: UK

Re: Need help breaking text file with multi lines into individual one line text files

Post by mkayyyy »

Zoranj wrote: Fri Oct 04, 2019 1:37 am Awesome help Mathew, thank you very much.

Not to be too greedy, but can you edit add function to remove blank lines as well? :oops:
No problem, sure that's pretty straight forward:

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job ) {
    // Construct file object from path of incoming job
    var inFile = new File(job.getPath());

    // Open the file and read contents into array
    inFile.open(File.ReadOnly);
    var lines = inFile.readLines();
    inFile.close();
	
	// Create a counter variable for file name suffix
	var counter = 1;
	for (var i = 0; i < lines.length; i += 1) {
		// Check that current line isn't blank
		if (lines[i] !== "") {
		    // Create temp path 
			var lineFileName = job.getNameProper() + "_" + (counter < 10 ? "0" + counter : counter) + ".txt";
			var lineFile = job.createPathWithName(lineFileName);
			
			// Write contents of current line to temporary file
			File.write(lineFile, lines[i], "UTF-8");
			
			// Send temporary file to outgoing connection
			job.sendToSingle(lineFile);
			
			// Add one to counter
			counter += 1;
		}
	}    

    // Send incoming job to null
    job.sendToNull(job.getPath());
}
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need help breaking text file with multi lines into individual one line text files

Post by Zoranj »

This works like a charm.
Much appreciated Mathew.

Saved me some significant time.
Let me know if I can repay somehow.
Post Reply