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

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

Guide ,
Aug 28, 2025 Aug 28, 2025

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

 

 

TOPICS
How to , Scripting
318
Translate
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 , Aug 29, 2025 Aug 29, 2025

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);
    }
}
Translate
Community Expert ,
Aug 29, 2025 Aug 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.

Translate
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
Guide ,
Aug 29, 2025 Aug 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);

Translate
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 ,
Aug 29, 2025 Aug 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

 

 

Translate
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
Guide ,
Aug 29, 2025 Aug 29, 2025

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

 

Translate
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 ,
Aug 29, 2025 Aug 29, 2025

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);
    }
}
Translate
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
Guide ,
Aug 29, 2025 Aug 29, 2025

The same issue occurs: the folder attachment is not copied, the process is interrupted, and an error pops up.
Without the folder, everything works fine.

0110.png

Translate
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 ,
Aug 30, 2025 Aug 30, 2025

Show the code you are running that throws the error

Translate
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
Guide ,
Aug 30, 2025 Aug 30, 2025

Hi rob day.

I'm using this exact script you provided.
This script does not support deleting folders.

Translate
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 ,
Aug 30, 2025 Aug 30, 2025

This script does not support deleting folders.

 

The script I provided isn’t deleting any files, it’s copying the files. If you are trying to delete files show us the code that is throwing the error.

Translate
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
Guide ,
Aug 30, 2025 Aug 30, 2025

HI @rob day 

Only deleted the file; everything is working fine.

That's enough. I'm giving up on deleting the folder.

Thank you very much,.

Translate
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 ,
Aug 31, 2025 Aug 31, 2025
LATEST

@dublove said: "… That's enough. I'm giving up on deleting the folder."

First the folder has to be all empty before you will be able to delete it. All empty also means no subfolders or files are there; even invisible files should not be there.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

Translate
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 ,
Aug 30, 2025 Aug 30, 2025

Hi @dublove ,

when the error is "copy is not a function" it might be that fs[i] is not a file.

Also check if the extra slash "/" is already part of destinationPath.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Translate
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