Renaming a File in ACC using Javascript

Avatar

Avatar
Validate 1
Level 1
4uSwan
Level 1

Likes

0 likes

Total Posts

4 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Ignite 1
View profile

Avatar
Validate 1
Level 1
4uSwan
Level 1

Likes

0 likes

Total Posts

4 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Ignite 1
View profile
4uSwan
Level 1

02-07-2021

The file name has a prefix of tmp.  I need to remove the prefix from the name.  I was trying to use the split and join method to break up the name and rejoin it again with out the tmp.  But I'm not sure these commands work in Adobe.  Below is the code I have written so far.  What am I doing wrong?

 

logInfo(vars.filename);

 

vars.nameOnly = vars.filename.rmdir();

vars.f1 = vars.nameOnly.split("_");

vars.f2 = f1.shift();

vars.finalName = f2.join("_");

 

logInfo(vars.finalName);

 

vars.cmd = path + vars.finalName;

 

execCommand(cmd);

 

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Affirm 3
Level 2
Krishnanunni
Level 2

Likes

5 likes

Total Posts

21 posts

Correct reply

2 solutions
Top badges earned
Affirm 3
Boost 5
Contributor
Shape 1
Boost 3
View profile

Avatar
Affirm 3
Level 2
Krishnanunni
Level 2

Likes

5 likes

Total Posts

21 posts

Correct reply

2 solutions
Top badges earned
Affirm 3
Boost 5
Contributor
Shape 1
Boost 3
View profile
Krishnanunni
Level 2

04-07-2021

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!

Answers (2)

Answers (2)

Avatar

Avatar
Validate 1
MVP
kapilKochar
MVP

Likes

128 likes

Total Posts

144 posts

Correct reply

44 solutions
Top badges earned
Validate 1
Contributor
Give Back 10
Give Back 5
Give Back 3
View profile

Avatar
Validate 1
MVP
kapilKochar
MVP

Likes

128 likes

Total Posts

144 posts

Correct reply

44 solutions
Top badges earned
Validate 1
Contributor
Give Back 10
Give Back 5
Give Back 3
View profile
kapilKochar
MVP

04-07-2021

Hi ,

Can you please add below function and try if it's works. I am using this function to rename my csv file using JS script in workflow and it's working fine for me. 

 

function test(oldName,newName)

{

try

{

var temp= new File(oldName);

temp.renameTo(newName);

}

catch(e)

logInfo("error " + e)

}

}

Avatar

Avatar
Affirm 100
MVP
_Manoj_Kumar
MVP

Likes

115 likes

Total Posts

328 posts

Correct reply

102 solutions
Top badges earned
Affirm 100
Validate 1
Contributor
Ignite 1
Shape 1
View profile

Avatar
Affirm 100
MVP
_Manoj_Kumar
MVP

Likes

115 likes

Total Posts

328 posts

Correct reply

102 solutions
Top badges earned
Affirm 100
Validate 1
Contributor
Ignite 1
Shape 1
View profile
_Manoj_Kumar
MVP

03-07-2021

Hello @4uSwan 

Did you try using fileRename function?

more details are given here:

https://docs.adobe.com/content/help/en/campaign-classic/technicalresources/api/f-fileRename.html