Skip to main content
Inspiring
April 27, 2019
Answered

Copy files on hard drive throught script code

  • April 27, 2019
  • 1 reply
  • 4064 views

Hi guys,

i have an array populated with media that i would like to copy in another folders on hard drive. Is possible throught scripting to copy files on disk?

Thank you

This topic has been closed for replies.
Correct answer Justin Taylor-Hyper Brew

Yes, if your CEP panel has Node.js enabled, you can copy files with fs.copyFile() or fs.copyFileSync()

var fs = require('fs');

// Sync Method

fs.copyFileSync(sourcePath, destinationPath);

// Async Method

fs.copyFile(sourcePath, destinationPath, function(err){

    if (err) throw err;
  console.log('File Copied');

});

If you're looking for an ExtendScript solution, you can use file.copy() on a file object.

var myFile = new File(pathToFile);

myFile.copy(pathToNewFile);

Node.js is typically faster if you can use that method.

1 reply

Justin Taylor-Hyper Brew
Community Expert
Community Expert
April 27, 2019

Yes, if your CEP panel has Node.js enabled, you can copy files with fs.copyFile() or fs.copyFileSync()

var fs = require('fs');

// Sync Method

fs.copyFileSync(sourcePath, destinationPath);

// Async Method

fs.copyFile(sourcePath, destinationPath, function(err){

    if (err) throw err;
  console.log('File Copied');

});

If you're looking for an ExtendScript solution, you can use file.copy() on a file object.

var myFile = new File(pathToFile);

myFile.copy(pathToNewFile);

Node.js is typically faster if you can use that method.

Cad81Author
Inspiring
April 28, 2019

I tryied to use the second solution, but the file.copy() return false and doesn't copy anything.

Here is my code:

var importFolder = new Folder;

importFolder = Folder.selectDialog("open a folder");

for(var t=0;t<video.length; t++)

{

var myFile = new File(video.projectItem.getMediaPath());

var response = myFile.copy(importFolder);

}

(in myFile i checked that there is a valid path)

Thank you

Justin Taylor-Hyper Brew
Community Expert
Community Expert
April 28, 2019

The argument for file.copy() is a path, not a folder object. So you'll need to get the path from your selected folder first like this:

var importFolder = new Folder;

importFolder = Folder.selectDialog("open a folder");

importFolder = importFolder.fsName;