Skip to main content
dublove
Legend
Nov 7, 2026 5:07 AM UTC
Answered

The script for recursively copying files and folders has some issues.

Hello everyone.

The original script could only copy files and reported an error when encountering a folder.


Later, I found this online, but it seems to be JS, which is still very different from ExtendeScrip.
For example, the original traversal of the file 'var items=srcFolder. getFiles();' seems incorrect.

I tried it, but it didn't work. I don't know where the problem lies.

Can you help me take a look?
Thank you very much.


var src = "D:\\aa"
var des = "D:\\bb"
copy(src, des);
/**
* Recursive Copy Folder
* @param {Folder} srcFolder
* @param {Folder} destFolder
*/
function copyFile(srcFolder, destFolder) {
if (!srcFolder.exists) {
alert("The source folder does not exist");
return;
}
// Retrieve all files and subfolders from the source folder
//var items = srcFolder.getFiles();
var items = Folder(srcFolder).getFiles();
for (var i = 0; i < items.length; i++) {
var item = items[i];
var destItem = new File(destFolder.fsName + "/" + item.name);
// If it is a folder, recursively call
if (item instanceof Folder) {
copyFile(item, new Folder(destFolder.fsName + "/" + item.name));
}
// If it is a file, perform a copy operation
else if (item instanceof File) {
item.copy(destItem.fsName);
}
}
}

 

    rob day
    rob dayCorrect answer
    Community Expert
    Nov 7, 2026 3:58 PM UTC

    Hi ​@dublove , Looks like your folder paths don’t exist. Using the default folder object paths make this a lot easier.

     

    I use this—the source and destination folders named aa and bb are on the desktop:

     

    //the source folder — a folder named "Test" on the desktop
    var s = Folder(Folder.desktop + "/aa")

    //the destination folder — a folder named "Copy" on the desktop
    var d = Folder(Folder.desktop + "/bb")

    //check that the souce and destination folders exist
    if (s.exists && d.exists) {
    //gets the nested files in the source folder
    var f = GetFiles(s)
    //copy only the InDesign files
    for (var i = 0; i < f.length; i++){
    if (f[i].type == "IDdD") {
    File(f[i]).copy(d + "/" + f[i].name)
    }
    };
    } else {
    alert("Check folder paths")
    }


    /**
    * Gets an array of nested files
    * @ param the folder with the files
    * @ return an array of files
    */
    function GetFiles(f) {
    var a = [],
    fl = f.getFiles(),
    i, file;
    for (i = 0; i < fl.length; i++) {
    file = fl[i];
    if (file instanceof Folder) {
    a = a.concat(GetFiles(file));
    }
    else if (file instanceof File) {
    a.push(file);
    }
    }
    return a;
    }

     

    9 replies

    rob day
    Community Expert
    rob dayCommunity ExpertCorrect answer
    Community Expert
    Nov 7, 2026 3:58 PM UTC

    Hi ​@dublove , Looks like your folder paths don’t exist. Using the default folder object paths make this a lot easier.

     

    I use this—the source and destination folders named aa and bb are on the desktop:

     

    //the source folder — a folder named "Test" on the desktop
    var s = Folder(Folder.desktop + "/aa")

    //the destination folder — a folder named "Copy" on the desktop
    var d = Folder(Folder.desktop + "/bb")

    //check that the souce and destination folders exist
    if (s.exists && d.exists) {
    //gets the nested files in the source folder
    var f = GetFiles(s)
    //copy only the InDesign files
    for (var i = 0; i < f.length; i++){
    if (f[i].type == "IDdD") {
    File(f[i]).copy(d + "/" + f[i].name)
    }
    };
    } else {
    alert("Check folder paths")
    }


    /**
    * Gets an array of nested files
    * @ param the folder with the files
    * @ return an array of files
    */
    function GetFiles(f) {
    var a = [],
    fl = f.getFiles(),
    i, file;
    for (i = 0; i < fl.length; i++) {
    file = fl[i];
    if (file instanceof Folder) {
    a = a.concat(GetFiles(file));
    }
    else if (file instanceof File) {
    a.push(file);
    }
    }
    return a;
    }

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 7, 2026 4:33 PM UTC

    Hi ​@rob day 

    I just want to copy all the content under aa (including subfolders).
    It's not just copying files.

    I found that adding the phrase 'create new subfolders' to my previous code is sufficient.

    // Copying subfolders is an essential step.
    if (!Folder(destFolder).exists) {
    df.create();
    }

     

    Translate
    Dipangana Chatterjee
    Community Manager
    Community Manager
    Nov 7, 2026 3:54 PM UTC

    Hi ​@dublove  Thank you for reporting the issue to us? may i know in which InDesign version, you are facing this issue?

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 7, 2026 4:35 PM UTC

    Hi ​@Dipangana Chatterjee 

    2024 19.5

    This has nothing to do with the version. All versions of InDesign have the issue of not being able to copy subfolders.

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 7, 2026 2:24 PM UTC

    Hi ​@rob day 

    Take a look at this copied folder.
    It seems that it still hasn't been successful.

    Translate
    Community Expert
    Nov 7, 2026 5:10 AM UTC

    I don’t see this different from extendscript. Anyhow, did not work is a proper explanation. What did not work? Did it crash? Did it work but not as per what you want? Provide details

    Translate
    -Manan
    dublove
    dubloveAuthor
    Legend
    Nov 7, 2026 7:11 AM UTC

    Hi ​@Manan Joshi 

    Sorry, I made a mistake in my original code.
    But for some reason (! SrcFolder. lists) did not make a correct judgment, as srcFolder does exist.
    I modified the last line and can copy the language file, but there is still no response to the folder.




    var src = "C:\\aa"
    var des = "C:\\bb"
    copyFile(src, des);
    /**
    * Recursive Copy Folder
    * @param {Folder} srcFolder
    * @param {Folder} destFolder
    */
    function copyFile(srcFolder, destFolder) {
    //if (!srcFolder.exists) {
    // alert("The source folder does not exist");
    // return;
    //}
    // Retrieve all files and subfolders from the source folder
    //var items = srcFolder.getFiles();
    var items = Folder(srcFolder).getFiles();
    for (var i = 0; i < items.length; i++) {
    var item = items[i];
    var destItem = new File(destFolder.fsName + "/" + item.name);
    // If it is a folder, recursively call
    if (item instanceof Folder) {
    copyFile(item, new Folder(destFolder.fsName + "/" + item.name));
    }
    // If it is a file, perform a copy operation
    if (item instanceof File) {
    //item.copy(destItem.fsName);
    item.copy(destFolder + "/" + item.name);
    }
    }
    }

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 7, 2026 9:42 AM UTC

    Does not support. fsName?
    It seems like removing this would be correct?

    But why is it wrong to determine if the file exists?
    My destFolder is a real existence.

    if (!srcFolder.exists) {
    alert("The source folder does not exist");
    return;
    }

     

    Translate