Extendscript是否可以使用文件对象(File。)复制文件?

时间:2019-07-12 01:49:24

标签: adobe extendscript

要在具有新文件扩展名的本地目录中复制文件。我看不到任何有关使用文件对象复制文件的文档。

我看到了File.copy()等功能,但是如果没有带有新名称和扩展名的对话框,则与复制或保存无关。

var targetFile = new File('myFile');
targetFile.saveDlg('newFileName' + 'extension');

1 个答案:

答案 0 :(得分:0)

要通过代码进行操作,您必须更加明确。您可以尝试以下方法:

function duplicateFile(path) {
  var content, extension, file, fileOk, name, newFile, newPath;
  file = new File(path);
  if(!file) {
    return
  }

  fileOk = file.open('r');

  if(fileOk){
    //Get file extension
    name = file.name.split('.');
    extension = name.pop();
    name.join('.');

    //Creating new file
    //Becareful with the name, you must to check that a file with the same name doesn't exists
    //if you don't want to overwrite it.
    name = name + '_copy.' + extension 
    newPath = file.parent.fsName + '/' + name
    newFile = new File(newPath);
    fileOk = newFile.open('w');

    //Writing content to new file
    if (fileOk) {
      newFile.write(content);
      newFile.close(); //Remember to close the files
    }
    file.close()
  }
}