Last active
December 31, 2015 04:59
-
-
Save suntong/7938320 to your computer and use it in GitHub Desktop.
Find & replace in files within a folder using Google Apps Script http://stackoverflow.com/questions/20510314/find-replace-in-files-within-a-folder-using-google-apps-script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Iterate through the file iterator for a particular folder | |
function iterateFolderById() { | |
var files = DriveApp.getFolderById("0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ").getFiles(); | |
while (files.hasNext()) { | |
var file = files.next(); | |
Logger.log(file.getName()); | |
var doc = DocumentApp.openById(file.getId()); | |
doc.replaceText("My search string or regex", "My replacement string"); | |
} | |
Logger.log("Done") | |
} | |
// Find folder by its name | |
function processFoldersByName() { | |
var folders = DriveApp.getFoldersByName("myfoldername"); | |
var myFolder = null; | |
if (folders.hasNext()) | |
myFolder = folders.next(); | |
if (myFolder !== null) { | |
// do stuff | |
} else { | |
// exit gracefully | |
} | |
} | |
// if you have multiple folders with the same name, you would have to iterate through them in a while loop | |
// (similar to the file iterator in the code you linked) and find a marker that proves this is the folder | |
// you are looking for (i.e. you could have an empty file with a particular name in there) | |
// By patt0, http://stackoverflow.com/questions/20510314/find-replace-in-files-within-a-folder-using-google-apps-script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment