Navigate:
This is a very particular sort of circumstance, but essentially I was trying to streamline a file ingestion process from an SFTP environment.
These files potentially would be millions of records long, and of a different format every time - so a new sample file would be necessary.
Conceptually, I wanted to: take a file accessed via the File Collector activity, pull the first 50 lines, put them into a new file, which is saved to the same location as the original file with some sort of change to the file name to indicate it’s a new record.
FINAL CODE:
var sourceText = vars.filename; var index = sourceText.split("."); var trimmedFile = index[0]; var newFileName = trimmedFile + "_sample.csv"; var fileA = new File (newFileName) fileA.open("w"); var fileB = new File (vars.filename); fileB.open(); var counter = 0; for each (var line in fileB){ if(counter > 50) {break;} counter++; fileA.writeln(line)} fileB.close(); fileA.close(); instance.vars.sampleFile = newFileName; instance.vars.loadedFile = vars.filename;
BREAKDOWN:
var sourceText = vars.filename; var index = sourceText.split("."); var trimmedFile = index[0]; var newFileName = trimmedFile + "_sample.csv";
1: The full file path of the file accessed by the File collector is declared to vars.filename, so it’s saved to a JS variable here
2: The file path in the first variable is split on the full stop into an array, meaning the index variable contains [”/sftp/path/folder/filename_goeshere”,”.csv”]
3: Accessing the array, the first item, is saved to a variable. (Arrays are 0 indexed, so 0 will return the first result).
4: Basic string concatenation to update the name, so it’ll be the same as the original file, but now with _sample at the end.
var fileA = new File (newFileName) fileA.open("w"); var fileB = new File (vars.filename); fileB.open();
1: Create an entirely new file in the SFTP environment, based on the file name/path we have defined above.
2: Open the new file within the JS environment in write mode. With no parameters passed, it’ll default to “read” mode and be unable to have data included.
Modes | Parameter | Behaviour |
Write | W | Access and overwrite information in a file |
Append | A | Add new information to the end of the file |
Read | R (or none) | Read information, no write ability |
3: Access the source file in the JS environment
4: Open the source file.
var counter = 0; for each (var line in fileB){ if(counter > 50) {break;} counter++; fileA.writeln(line)}
1: Create a variable to log how many lines we have added to the new file.
2: Start a loop based on lines within the source file.
3: First step for each loop, check to see if the counter is less than 50. If it is not? End the loop.
4: Next, add 1 to the counter.
5: Finally, write the current line from the Source file to the new Sample file.
fileB.close(); fileA.close(); instance.vars.sampleFile = newFileName; instance.vars.loadedFile = vars.filename;
1: Once the loop ends, close the original file.
2: Close the sample file.
3: Save the file path for the new file to an instance variable
4: Save the file path for the original file (just in case).
So once you’ve run this, you have a sample file ready to use! I’m working on seeing if there’s a good way to dynamically access this for a data loading activity, but it doesn’t look super promising.