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

Copy files on hard drive throught script code

Participant ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

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

TOPICS
SDK

Views

3.4K

Translate

Translate

Report

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 , Apr 27, 2019 Apr 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 us

...

Votes

Translate

Translate
Community Expert ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Participant ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

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
Participant ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

Mhh, it doesn't work.

I used .fsName and now i have the right path of the target folder, but it doesn't copy the files.

I also tried (for test only) using direct file path like "f:\work\video.mp4" but it still not working.

Thank you..

Here is the code:

var importFolder = new Folder;

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

importFolder = importFolder.fsName;

var myFile = new File("f:\work\video.mp4");

myFile.copy(importFolder);

Votes

Translate

Translate

Report

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 ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

Firstly, you need to use double backslashes in PC paths to represent a single backslash, otherwise Javascript/Extendscript thinks you're trying to escape characters.

Secondly, you need the full path to the file, not the folder.

var importFolder = new Folder;

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

var myFile = new File("F:\\work\\video.mp4");

var newFileName = importFolder.fsName + '\\' + 'copied_' + myFile.name;    // creates the full path to the new file

myFile.copy(newFileName);

Votes

Translate

Translate

Report

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
Participant ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

Awesome! A huge thanks Justin, you helped me a lot

Votes

Translate

Translate

Report

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 ,
Apr 27, 2019 Apr 27, 2019

Copy link to clipboard

Copied

Glad it worked, happy to help!

Votes

Translate

Translate

Report

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
Participant ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

LATEST

Hi

as you have imagined, i'm not a coder, but i have a basic knowledge, so now i'm a bit confused. I read the links you have posted in my other discussion and looked for others, but it's too much complicated for my actual skills.

Reading, i found this information, but i don't know if it's correct:

I have to add in the "manifest.xml", in the <resource> tag the following lines:

<CEFCommandLine>

<Parameter>--enable-nodejs</Parameter>

</CEFCommandLine>

and then use the code of your first answer...Is it correct or is a bit more complicated?

Thank you

Votes

Translate

Translate

Report

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