• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

I want to copy and past contents of folder with ESTK

Explorer ,
Feb 13, 2024 Feb 13, 2024

Copy link to clipboard

Copied

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.
TOPICS
FAQ

Views

74

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 14, 2024 Feb 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 frame
...

Votes

Translate

Translate
Community Expert ,
Feb 14, 2024 Feb 14, 2024

Copy link to clipboard

Copied

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);
        }        
    }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2024 Feb 14, 2024

Copy link to clipboard

Copied

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);                    
                }
            }
        }
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines