Expand my Community achievements bar.

Javascript

Avatar

Level 1

I'm trying to merge multiple CSV files into one and archive the originals using javascript in Adobe Campaign. The program runs without errors but doesn't seem to actually do anything. Can anyone help figure out why? Is this particular javascript code actually supported by adobe campaign? I have already checked the file paths and they are correct. 

 

If my code is needed I will gladly post it, but first I need to know if these functions are even possible to use in javascript in adobe campaign. 

 

Edit: Here is my code that I used:

 

 

// Defining the source and the archive directory

var home = '\\\\home\\files;

var dir = new File(home);

var files = dir.list("test_file_*.csv");

 

instance.vars.archiveDir = 'D:\\home\\files\\archive';

 

// Create a new combined file in the source directory

var combinedFile = new File(home + '\\combined.csv');

 

for each (var file in files) {

  var filePath = home + '\\' + file.name;

 

  // Check if file name contains the specific phrase

  if (file.name.indexOf('test_file_') > -1) {

    var data = File(filePath).read(); // Read each file content

    combinedFile.write(data); // Write the content to the combined file

   

    var archivePath = instance.vars.archiveDir + file.name;

    file.move(archivePath); // Move file to archive

  }

}

 

if (combinedFile.exists) {

  logInfo('Combined file created at: ' + combinedFile.path);

}

 

 

5 Replies

Avatar

Community Advisor

Hi @Ace12,

 

There are multiple ways you can combine multiple csv files into a single file in ACC.

 

1. Write js code and use File class methods, such as open, write, etc. Here you can create a copy of one of the files, open that copy in append mode [file.open("a")], open the second file in read mode, read it line by line and write it into the copied first file line by line. More details about the methods can be found here: https://experienceleague.adobe.com/developer/campaign-api/api/c-File.html

 

2. Use commands (unix or windows) depending on your server's operating system to do the same operations in the execCommand method available in js code.

 

3. Read the two files in data loading activities and merge them in a union activity, and finally extract them into a third file. This method is my go-to since reading/writing files in js code can be resource expensive and lead to memory issues on the server, and using OS level commands is not platform agnostic.

 

I hope this information helps.

 

If you want to do it the way you are trying now, please share the code and the peers here can take a look and guide you.

 

BR,

Ishan

Avatar

Level 1

Thank you so much for your thorough reply Ishan! I really appreciate your help. I have uploaded my code in the original post. Im also posting it in this reply for more visibility. Also if this has already been asked before, thanks for going easy on me, this is my first post haha!

 

// Defining the source and the archive directory

var home = '\\\\home\\files;

var dir = new File(home);

var files = dir.list("test_file_*.csv");

 

instance.vars.archiveDir = 'D:\\home\\files\\archive';

 

// Create a new combined file in the source directory

var combinedFile = new File(home + '\\combined.csv');

 

for each (var file in files) {

  var filePath = home + '\\' + file.name;

 

  // Check if file name contains the specific phrase

  if (file.name.indexOf('test_file_') > -1) {

    var data = File(filePath).read(); // Read each file content

    combinedFile.write(data); // Write the content to the combined file

   

    var archivePath = instance.vars.archiveDir + file.name;

    file.move(archivePath); // Move file to archive

  }

}

 

if (combinedFile.exists) {

  logInfo('Combined file created at: ' + combinedFile.path);

}

Avatar

Community Advisor

Hi @Ace12,

 

In your code you are not opening any of the files using the File.open() method. You must also supply as argument the mode in which you want to open the files.

e.g. you might want to open the files for reading in read mode - File.open("r");

The file in which you want to combine other files' data should be opened in append mode - File.open("a");

Then you should be able to write into that file as you want.

 

Let me know if that works.


BR,

Ishan

Avatar

Level 1

Hey Ishan,

 

Thank you again for replying I very much appreciate it. 

 

This is the new code i used. 

// Create a new combined file in the source directory

var combinedFile = new File(home + '\\combined.csv');

 

for each (var file in files) {

  var filePath = home + '\\' + file.name;

 

  // Check if file name contains the specific phrase

  if (file.name.indexOf('testing_file_') > -1) {

    var fileToRead = new File(filePath);

    fileToRead.open('r'); // Open the file in read mode

    var data = fileToRead.read();

    fileToRead.close();

 

    combinedFile.open('a'); // Open the combined file in append mode

    combinedFile.write(data); // Write the content to the combined file

    combinedFile.close();

   

    var archivePath = instance.vars.archiveDir + file.name;

    file.move(archivePath); // Move file to archive

  }

}

 

if (combinedFile.exists) {

  logInfo('Combined file created at: ' + combinedFile.path);

}

 

It ran fine in Adobe, but nothing actually happens unfortunately. 

Avatar

Level 1

Is there anyone who can help me with this problem?