Skip to main content
Known Participant
February 14, 2024
Answered

I want to copy and past contents of folder with ESTK

  • February 14, 2024
  • 2 replies
  • 279 views

Hello,

to change template, I'm trying to use ESTK scripts.
I made scripts

// Function to copy the contents of a folder to another folder
function copyFolderContents(sourceFolder, destinationFolder) {
    // Get a list of files and subfolders in the source folder
    var files = sourceFolder.getFiles("*");
    var subfolders = sourceFolder.getFiles("*");
 
    // Copy each file from the source folder to the destination folder
    for (var i = 0; i < files.length; i++) {
        var file = files[i];
        var destinationFile = new File(destinationFolder + "/" + file.name);
        file.copyTo(destinationFile);
    }
 
    // Recursively copy each subfolder from the source folder to the destination folder
    for (var j = 0; j < subfolders.length; j++) {
        var subfolder = subfolders[j];
        var destinationSubfolder = new Folder(destinationFolder + "/" + subfolder.name);
        copyFolderContents(subfolder, destinationSubfolder);
    }
but it is not working.
I wish I can get tips for solving this problem.
This topic has been closed for replies.
Correct answer frameexpert

This requires "recursion" in order to work your way down the file/folder structure. Recursion is where a function calls itself. Here is an example that doesn't do any copying, but just writes the file and folder names from your source folder and subfolders to the FrameMaker Console. I will try to post a version that actually copies, but this will give you concept of touching each file and folder. You will have to edit the code and put in your own source and destination folders.

 

 

#target framemaker

var sourceFolder, destinationFolder;

sourceFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\20240205-TitleCase");
destinationFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\Copy");

processFolder (sourceFolder);

function processFolder (sourceFolder, destinationFolder) {

    var files, count, i;
    
    files = sourceFolder.getFiles ();
    count = files.length;
    for (i = 0; i < count; i += 1) {
        if (files[i] instanceof Folder) {
            Console ("folder: " + files[i].fsName);
            // Recursive call to this function:
            processFolder (files[i], destinationFolder);
        }
        else {
            Console ("* " + files[i].fsName);
        }        
    }
}

 

2 replies

frameexpert
Community Expert
Community Expert
February 14, 2024

This version actually does the copying and creates target folders where necessary. I renamed the destinationFolder variable to targetFolder.

 

var sourceFolder, targetFolder;

sourceFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\20240205-TitleCase");
targetFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\Copy");

processFolder (sourceFolder, targetFolder);

function processFolder (sourceFolder, targetFolder) {
    
    var files, count, i, source, target;
    
    // Get all of the files and folders from the source folder.
    files = sourceFolder.getFiles ();
    count = files.length;
    for (i = 0; i < count; i += 1) {
        // Get the source file object and its target object.
        source = files[i];
        target = new File (targetFolder.fsName + "\\" + source.name);
        // See if the source is a File or Folder.
        if (source instanceof File) {
            // Copy the file to the target folder.
            source.copy (target);
        }
        else { // Folder
            target = new Folder (target);
            // If the folder exists, call this function.
            if (target.exists === true) {
                processFolder (source, target);
            }
            else {
                // Create the target folder and call this function.
                if (target.create () === true) {
                    processFolder (source, target);                    
                }
            }
        }
    }
}
Known Participant
February 27, 2024

Thank you for tips, It really hepls me.
Have a nice day always.

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
February 14, 2024

This requires "recursion" in order to work your way down the file/folder structure. Recursion is where a function calls itself. Here is an example that doesn't do any copying, but just writes the file and folder names from your source folder and subfolders to the FrameMaker Console. I will try to post a version that actually copies, but this will give you concept of touching each file and folder. You will have to edit the code and put in your own source and destination folders.

 

 

#target framemaker

var sourceFolder, destinationFolder;

sourceFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\20240205-TitleCase");
destinationFolder = new Folder ("C:\\DATA-10\\Scripts\\ArtCampbell\\Copy");

processFolder (sourceFolder);

function processFolder (sourceFolder, destinationFolder) {

    var files, count, i;
    
    files = sourceFolder.getFiles ();
    count = files.length;
    for (i = 0; i < count; i += 1) {
        if (files[i] instanceof Folder) {
            Console ("folder: " + files[i].fsName);
            // Recursive call to this function:
            processFolder (files[i], destinationFolder);
        }
        else {
            Console ("* " + files[i].fsName);
        }        
    }
}