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?
Asked
Active
Viewed 77 times
1
-
1Although 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 Answers
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
-
1Oh, looks like there's a similar answer already available here - https://stackoverflow.com/a/46135473/10713297 – Sourabh Choraria Sep 27 '19 at 14:11
-
Thanks! If I understand correctly, this is removing the files in the root folder and is creating copies inside the specified folder? – orschiro Sep 27 '19 at 15:28
-
-
@orschiro - I'm curious :) Were you able to find something that did not leave copies? – Sourabh Choraria Oct 06 '19 at 09:05