Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a file in a folder?

I have an Apps Script that creates multiple files and moves them into a folder. Is there a way to place the a file directly in a folder or do I first have to get the file then copy it to the appropriate folder and then removing it from the root folder like this:

folder=DocsList.createFolder("MyFolder");
var file=DocsList.createFile(blob);
file.addToFolder(folder);
file.removeFromFolder(DocsList.getRootFolder());

The problem with this is that if you open up Drive you see the file is first placed in the root folder then moved to MyFolder. So there is a bit of a lag until the execution of removeFromFolder.

like image 423
Diaa Avatar asked Aug 11 '12 00:08

Diaa


People also ask

How do you create a new file in files?

Click on File Manager in the left panel. Select the directory for the new folder, then New and New File. Name the new file and choose the extension type you want. Your new file is now visible.

How do you create a folder and save a file in it?

With your document open, click File > Save As. Under Save As, select where you want to create your new folder. You might need to click Browse or Computer, and navigate to the location for your new folder. In the Save As dialog box that opens, click New Folder.


2 Answers

The DocsList no longer works and has been replaced by the DriveAPI , Google App Services and Advanced Google services .

Here I leave my test code , this code creates folders - subfolders - google files - pdf - and stores them in folders and subfolders in various ways, and I hope will be helpful

function Drive_2015() {   //busca un folder, si no lo hay lo crea y crea 2 sub carpetas un google document copiado en cada una de ellas
  var name='folder de prueba';
  var carpeta = DriveApp.getRootFolder().searchFolders("title contains '"+name+"'");
  if (carpeta.hasNext()===true) {    
      while (carpeta.hasNext()) {
      var folder = carpeta.next();
      Logger.log(folder.getName()+' '+folder.getId());
      }
  }else{
    var folder=DriveApp.getRootFolder().createFolder(name);

    var parent=DriveApp.getFolderById(folder.getId());   // get parent folder
    var folder2 =parent.createFolder('Subfolder');   // 1° way to create sub folder
    var folder3=folder.createFolder(name+1);// 2° way to create sub folder (and more easy)

    var doc = DocumentApp.create('Documento sta');
    var sheet = SpreadsheetApp.create('Spreadsheet sta');
    Utilities.sleep(300); // este retardo es para garantizar en el user-side la creacion del nuevo archivo
    var files = DriveApp.getFilesByName('Documento sta');// or Id var file = DriveApp.getFileById(doc.getId());
    while (files.hasNext()) {
      var file = files.next();
      Logger.log('ojo '+file.getName());
      file.makeCopy(folder3);
      file.makeCopy(folder2);
      var file2=file.makeCopy(folder);
      file2.setName('Acta individual del alumno')
      var blob = file2.getAs('application/pdf'); 
      var file2pdf = folder.createFile(blob);
      var file2pdf = DriveApp.getFileById(file2pdf.getId());
      var file2pdf=file2pdf.makeCopy(folder);
      Logger.log('se creó: '+file.getName()+' en la carpeta: '+folder.getName()+' el PDF es: '+file2pdf.getId());
      DriveApp.getFileById(file2pdf.getId()).setTrashed(true)
      DriveApp.getFileById(docid).setTrashed(true)
      }
  }
}
like image 199
Stanley Illidge Avatar answered Oct 10 '22 05:10

Stanley Illidge


Just tested the following code

function Test() {
  DocsList.createFolder('Folder1').createFolder('Subfolder1').createFile('File1', 'Empty');
}

It works as expected, i.e. created a new File1 document in newly created folder My Drive\Folder1\Subfolder1.

like image 29
megabyte1024 Avatar answered Oct 10 '22 05:10

megabyte1024