Google Script for Moving Files in Drive


#1

Hi All,

I made a script for automatically moving files around in Drive. I my case I wanted PDFs uploaded through Scanbot Pro moved from one folder to another based off the name (as defined by a search term in the script. I wanted anything that contained the term “bill” to be moved but everything else to be left alone. Anyways here is the link to the script example (personal info removed). and the copy and paste below if you don’t want to click through.

var sourceFolder = “YOURSOURCEFOLDERHERE”; //Enter source folder ID
var destinationFolder = “YOURDESTINATIONHERE”; //Enter destination folder ID
var searchTerms = 'SEARCHTERMSHERE '; //Enter search terms FORMATTING = https://developers.google.com/drive/api/v3/search-parameters

function moveFiles(){
var files = DriveApp.getFolderById(sourceFolder).searchFiles(searchTerms);
while (files.hasNext()) { //looking for files in sourceFolder
var file = files.next();
var destination = DriveApp.getFolderById(destinationFolder); //Find destination folder
destination.addFile(file); //Move the files
var pull = DriveApp.getFolderById(sourceFolder); //remove the files from the source folder
pull.removeFile(file);

}
}

I am not a programmer so there are probably better ways to do this, but I did try to make it easy to change. Simply add the folder IDs and search terms for your folders in the top variables and you are good to go. Here is the info for defining searches, and credit to this video for the base code. And the video shows you how to get the folder ID as well.

One last note for those unfamiliar, you have to set a trigger for the script once you make it. There is a clock in the menu of the script editor to do so. In this case a time trigger. I set mine to run every 15 mins, you can set it as needed.

I am curious if this is something like what Hazel does, but I am on Android/Windows/Google so let me know if that is the case. There are probably ways to extend this to to renaming files and such.

Finally, for those curious this completes an automation loop for me of Scanbot Pro automatically uploading a PDF named “Date Medical Bill” to my scans folder, then moving it to a bills folder, and finally IFTTT adding a reminder to Todoist to pay the bill. Anyways, I hope this helps anyone interested, and let me know if it can be improved!


#2

There’s some similarity to Hazel. Hazel hooks into the file system and so can trigger immediately once a file is in place. The sorts of actions are broadly comparable - the main difference being the UI vs. The scripting and being able to leverage local apps vs. only cloud based operations (apps, APIS, etc.) Google scripts do give some great options for super charging within the Google eco-system. :sunglasses:

It may be worth noting for some, that you can also trigger the script via a URL (if you are logged in) as well as on a timer. Before my employer moved to Office365, I used a Google script I wrote to file emails (something Hazel doesn’t do of course) using criteria well beyond the remit of the usual Gmail rules. My auto filer script ran a couple of times a day on a schedule, but after a heavy e-mail triage session I’d kick off the script manually to get things in order.


#3

Nice, did you file emails in a PDF or text form? If so that sounds like the Gmail2Drive script that I use, not for that specifically, but it can do that. I use it to save certain attachments in drive. I found that one on the internet and it has been awesome, especially since I could not have written it if I tried!

How do you trigger a script from a URL? I did not see that option.


#4

I was filing the e-mails within a folder structure within Gmail itself. There seemed little point in archiving mails out onto a Google file system from where it was. Whenever I needed a file based copy I simply saved as PDF from my mail client. Because of my filing approach and my option to use Gmail’s advanced search filters, retrieval was never an issue for me and backup was via a backup of my laptop as it kept a complete offline archive; internally I think the app was applying EML format to the local mail storage.

Good question on the running via URL. The key was to deploy it as a web app and use a doGet() function to initiate the run.

The script contains various functions to do different types of filing, deleting, archiving, etc. The doGet() basically just acted like a main() that you might find as the primary control in some languages. You call that, it calls whatever else needs to be done.

My use case was actually the simplest sort of trigger possible as I didn’t need to pass in any parameters. It just processed my mailbox in the same way every time it was called.