Skip to main content
dublove
Legend
August 28, 2025
Answered

Is it possible to copy all files under a specific relative path?

  • August 28, 2025
  • 1 reply
  • 527 views

For example: Copy all files under

‘..preSetting’ + ‘/bb/’

to

Folder.desktop + ‘/aa/’

My path structure is as follows, with “Script” at the same level as preSetting:
D:/OO/Script/myscript.jsx
D:/OO/preSetting/bb/

This one below seems wrong.

 

//var activeDocPath = String(app.activeDocument.fullName);
var copyToPath = String(Folder.desktop + '/aa/');
var sPath = String("/" + PreSetting + '/bb/');
//alert(sourcePath);
//alert(pShortcut);
var copiedFile = copyFile(sPath, copyToPath);
function copyFile(sourcePath, destinationPath) {
    var fp = Folder(sourcePath)
    //get the files in MyFiles
    var f = fp.getFiles();
    alert(f);
    alert(f.length);
    for (var i = 0; i < f.length; i++) {
        //try {
        if (!f[i].exists) {
            throw new Error('File not found: "' + sourcePath + '".');
        }
        else {
            var dup = File(destinationPath);
            f[i].copy(dup)
            return dup;
        }
    }
}

 

 

Correct answer rob day

Thank you very much.

The path issue has been resolved.
My script is located here: app.activeScript.parent.parent

 

The code below cannot copy directories.

If there is a folder, the line 'fs[i].copy(dup);' will report that there is no such function named 'copy()".

copyFile(sPath, copyToPath);
function copyFile(sourcePath, destinationPath) {
    var fd = Folder(sourcePath)
    var fs = fd.getFiles();
    for (var i = 0; i < fs.length; i++) {
        var dup = File(destinationPath + "/" + fs[i].name);
        alert(dup);
        fs[i].copy(dup);
    }
}

 


Try this:

 

//Folders named Source and Dest on the desktop
copyFile(Folder.desktop + "/Source", Folder.desktop + "/Dest");
function copyFile(sourcePath, destinationPath) {
    var fs = Folder(sourcePath).getFiles();
    for (var i = 0; i < fs.length; i++) {
        fs[i].copy(destinationPath + "/" + fs[i].name);
    }
}

1 reply

Community Expert
August 29, 2025

Hi @dublove ,

look into DOM documentation for object File and method copy(). There you'll see that the argument in copy() is not just a folder path, but a string describing a file's path ( or a File object ). So in essence you have to add the file name to the target's folder path and do:

copy( destinationPath +"/"+ f[i].name )

What also should work is:

copy( File( destinationPath +"/"+ f[i].name ) )

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#File.html#d1e6347__d1e6984

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

PS: Edited post.

dublove
dubloveAuthor
Legend
August 29, 2025

@Laubender 

How do I represent the relative path to bb?
My source file's relationship to the current script:
D:/OO/Script/myscript.jsx
D:/OO/pre/bb/

 

Is that how it works?
var myScript = app.activeScript;
alert(myScript.parent.parent);

rob day
Community Expert
Community Expert
August 29, 2025

This would get the path to the script’s folder:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#$.html

 

var sf = File($.fileName).parent
alert("This script’s parent folder: \r\r" + sf)
//returns /Applications/Adobe%20InDesign%202024/Scripts/Scripts%20Panel