1

Is there a way to move all files inside the root of Google Drive into a specified folder such as shown below without creating copies of the to be copied files?

enter image description here

orschiro
  • 19,847
  • 19
  • 64
  • 95
  • 1
    Although I'm not sure whether I could correctly understand about `efficient` you are thinking, if `efficient` is the process cost, how about using [Drive API with the batch request](https://developers.google.com/drive/api/v3/batch)? By this, the process cost can be reduced. If `efficient` is the number of use of APIs, how about using [Drive Service](https://developers.google.com/apps-script/reference/drive)? By this, you can move the files without using API quotas. If I misunderstood your question, I apologize. – Tanaike Sep 27 '19 at 14:08
  • If you are still looking for the solution, can I ask you about your current issue? – Tanaike Sep 29 '19 at 23:44
  • I prefer to have my root folder clean and want to periodically move all files into a folder to not clutter up root. However, some of those files are shared, so I can't delete them and create a copy. Need to move without deleting files. – orschiro Sep 30 '19 at 08:17
  • Thank you for replying. From your replying, I think that the existing answer can resolve your issue. For example, if there are a lot of files in the root folder, I recommend to use Drive API and the batch request. But I cannot understand about your situation. So I had asked about it. – Tanaike Sep 30 '19 at 22:21

1 Answers1

1

Try this -

function myFunction() {
  var files = DriveApp.getRootFolder().getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var targetFolder = DriveApp.getFolderById('FolderIDGoesHere').addFile(file);
    DriveApp.getRootFolder().removeFile(file);
    Logger.log(file.getName());
  }
}
Sourabh Choraria
  • 2,255
  • 25
  • 64