0
votes

I'm using Google Drive and I'd like to Archive all the files synced with my computer which are older than a certain amount of time. As a result, the archived files will disappear from my computer on the next sync.

I've found several scripts to perform the file moving but it seems challenging to move the file and keep the path structure as if it was moved using a robocopy/powershell command locally on the computer.

any idea if such feature is possible with Google Apps Scripts?

So far, I haven't integrated yet the copy/remove function. I'm retrieving the folder path and I guess there must be a way to check if path already exists in destination and if not, create subfolders?

function FilesModifiedLastWeek(){

// Find files modified last week

  var today     = new Date();
  var oneDayAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);  
  var startTime = oneDayAgo.toISOString();

  // The magic search expression
  var search = '(trashed = true or trashed = false) and (modifiedDate > "' + startTime + '")';
  var files  = DriveApp.searchFiles(search);

  // Loop through all the files in the search results
  while( files.hasNext() ) {

    var file = files.next();

    var fileName    = file.getName();
    var parentFolder = file.getParents();
    folders = [];

    //Get full path
    while (parentFolder.hasNext()) {
      parentFolder = parentFolder.next();
      var title = parentFolder.getName();
      folders.push(parentFolder.getName());
      parentFolder = parentFolder.getParents();
    }
    var fileURL     = file.getUrl();
    var dateCreated =  Utilities.formatDate(file.getDateCreated(), "Europe/Zurich", "yyyy-MM-dd HH:mm");
    Logger.log("Filename: " + fileName + " Folder path: " + folders.reverse().join("/") + " File creation date:" + dateCreated);
  }
}

Additional point, is it a way to exclude the Archive folder from the search? :)

1
Could you please clarify what do you mean by "keep the folder path"? Where is it supposed to be kept?Anton Dementiev
Hi, what I mean is, if the file to be moved if located under My Compter/LambdaFolder/AlphaSubFolder/Myfile.pdf, I'd like to store this file under Archive/LambdaFolder/AlphaSubFolder/Myfile.pdfpoypoy
The getFoldersByName(name) method can be called on the instance of the Folder class to get all child folders. If the FolderIterator returned by this method returns true after calling hasNext(), the folder already exists, if not - it must be created (that also means the whole path doesn't exist). You can repeat this process for the entire path and then move the file to the last folder and remove it from the previous parent. This will allow you to move the file without copying it. Is that what you are looking for?Anton Dementiev

1 Answers

0
votes

You can achieve this with the Advanced Drive Services and one extra function in your code.

We will be using the Patch command to add and remove parent folders.

Optional query parameters

addParents string A comma-separated list of parent IDs to add.

removeParents string A comma-separated list of parent IDs to remove.

The code is like this:

function placeFileInFolders(file, folders) {
  var archiveFolder = DriveApp.getFoldersByName("Archive Folder").next();
  
  var lastFolder = archiveFolder;
  for (var i=0; i<folders.length; i++) {
    if (lastFolder.getFoldersByName(folders[i]).hasNext()) {
      lastFolder = lastFolder.getFoldersByName(folders[i]).next();
    } else {
      lastFolder = lastFolder.createFolder(folders[i]);
    }
  }
  var newFile = Drive.Files.patch({}, file.getId(), {
    addParents: lastFolder.getId(),
    removeParents: file.getParents().next().getId()
  });
}

And you can use it like this:

    //...
    Logger.log("Filename: " + fileName + " Folder path: " + folders.reverse().join("/") + " File creation date:" + dateCreated);
    placeFileInFolders(file, folders.reverse());
    //...

Improvements

Shared with Me

I've also added one extra case where your code will be able to handle files "Shared With Me".

    //...
    if (!parentFolder.hasNext()) {
      folders.push("Shared With Me");
    }
    while (parentFolder.hasNext()) {
    //...

Delete empty folders

This script does not remove folders that have all their contents moved to the Archive.