Avatar

Correct answer by
Level 4

Hi @4uSwan ,

The split function actually returns an array of elements that consist of all the substrings that were next to a separator.
For eg, if you run the following code, 
var testString = "hello world for test" ;
var array = testString.split(" ");

 

The resulting array variable would have content as ["Hello","world","for","test"]

From the array, you can choose the desired data using the array[index] format.

 

In your case, var f1 = vars.nameOnly.split("_");
you get the f1 = ["tmp","rest_of_filename"]

to get the filename excluding the temp, you can use f1[1] to get the "rest of filename";

You can use the following code to rename a file in SFTP
var filepath = "/sftp/<path to file folder>/";
var oldFilename = "tmp_test.csv";
var newFilename = oldFilename.split("_")[1];
logInfo(newFilename);
var command = "mv -i "+filepath+oldFilename+" "+filepath+newFilename;
var result = execCommand(command);

Hope it helps!

View solution in original post