0

I'm building a photoshop extension and I want to clone a folder to another folder in the adobe files system (myDocuments or userData), I use the .copy() method but this one duplicate just the files, not subfolders, here's the structure of the source and destination folders: enter image description here

here's my basic code in the JSX file:

function Copier() {
  var src = "~/Desktop/main";
  var dest = "~/Desktop/Dest";

  if (src.exists) {
    var files = src.getFiles()
    for (var i = 0; i < files.length; i++) {
      files[i].copy(dest);
    }
  } else {
    return false;
  }
}
  • Sergey's solution should work. But if you need to preserve dates of the files you can do it with `file.execute()` function or run a system appleScript from Extendscript: https://stackoverflow.com/a/14445125/14265469 – Yuri Khristich Jun 22 '21 at 09:03

1 Answers1

0

Bu using a recursive function that would create folders for each folder and copy files for each file. Here's the function I use:

copyFolderContents({
  source: "/path/to/source",
  target: "/path/to/target",
  supressAlerts: true,
})

function copyFolderContents(data)
  {
    if (data.source.constructor != Folder) data.source = new Folder(data.source);
    if (data.target.constructor != Folder) data.target = new Folder(data.target);
    if (data.supressAlerts == undefined) data.supressAlerts = false
    var errors = false;

    copyFolder(data.source, data.target)

    function copyFolder(sourceFolder, destinationFolder)
    {
      var sourceChildrenArr = sourceFolder.getFiles();
      for (var i = 0; i < sourceChildrenArr.length; i++)
      {
        var sourceChild = sourceChildrenArr[i];
        var destinationChildStr = destinationFolder.fsName + "/" + sourceChild.name;
        if (sourceChild instanceof File)
        {
          copyFile(sourceChild, new File(destinationChildStr));
        }
        else
        {
          copyFolder(sourceChild, new Folder(destinationChildStr));
        }
      }
    }

    function copyFile(sourceFile, destinationFile)
    {
      createFolder(destinationFile.parent);
      if (!sourceFile.copy(destinationFile))
      {
        if (!data.supressAlerts)
        {
          alert('Unable to copy\n' + sourceFile + '\nto\n' + destinationFile + '\nAborting.');
          errors = true;
          return false;
        }
      };
    }

    function createFolder(folder)
    {
      if (folder.parent !== null && !folder.parent.exists)
      {
        createFolder(folder.parent);
      }
      if (!folder.create())
      {
        alert('Unable to create:\n' + folder + "\nAborting.");
        errors = true;
        return false;
      };
    }
    if (errors)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

Sergey Kritskiy
  • 2,199
  • 2
  • 15
  • 20