- [Narrator] At this point, our Google Drive is getting a little bit messy. Every time we run our script to convert our Google Doc into markdown, it's generating a new file at the root of our Google Drive. Whenever we work with creating dynamic files, it helps to organize these into folders. So let's take a look at how to create and work with new folders on Google Drive. We'll switch over to our code project and go into our Utils script. Next, let's create a new function, and we're going to call this getFolder.
We're going to pass in a name for the name of the folder, a boolean for whether we should automatically create the folder if we don't find it, and root for the location for where the folder should be searched for. Searching for folders on Google Drive works very similar to searching for files. Whenever we search for a file or folder by name, we will get an iterator. In this case, we're going to get a folder iterator that we can look through to find out if the folder we're looking for actually exists.
The first thing we want to do is find the location for where we should actually look for the folder. Let's go ahead and add a condition to test to see if root equals no. If no root is supplied, we're going to set the root equal to the drive app itself. This way, we can automatically search the root of the Google Drive, or create files at the root of the Google Drive if the folder doesn't exist. Next, let's create a new variable and call this folder iterator.
We're going to set this equal to the root dot getFoldersByName. As I'd mentioned, you'll see here that this is going to return a folder iterator. Let's go ahead and select this from the code completion dropdown. We'll pass in the name argument that goes into our getFolder function. Next, we're going to create a new variable for the folder we're looking for. We'll set this to no by default. Next, let's go ahead and test to see if the folder iterator has a next object.
If a next item exists, we're going to go ahead and set our folder variable equal to the folder iterator next item. Now before we can move on, there's still a chance that the folder might be set to no at this point. So let's go ahead and test to see if folder equals no, and let's also see if create is set to true. If this is true, we're going to set the value of folder equal to root.createFolder and pass in the name.
This ensures that we always get a folder back whenever we automatically set create to true. At this point, our method is done, so let's go ahead and return the reference to the folder we were looking for. It always helps to wrap the native file APIs within our own set of APIs. This way we can control the type of files we're looking for, as well as the results of what gets returned. We wouldn't want to add all this logic every time we need to get a new folder inside of our main script.
Let's go ahead and save our utilities script and go back into the main script. Now before we go ahead and create the document, let's create a new variable and call this newFolder. Here, we're going to set this equal to the returned results of our getFolder method. We're going to pass in the name MarkdownGen. We'll also pass in true, so that our folder is automatically created if it doesn't exist.
Now that we have the new folder, let's go ahead and create a subfolder. Every time we generate out a markdown file, we're going to add it into a subfolder of the MarkdownGen folder, and we'll add a time stamp to it. We'll set this value equal to getFolder, and let's call this Output with a dash +getTimeStamp. We'll want to autocreate this folder by setting the next argument to true, and we're going to create this folder inside of our new folder.
The last thing we're going to do is replace our call to DriveApp with subFolder. And let's change the markdown test file name back to markdown-test.md. This way every time we create a new markdown file, it has the same name, but it's instead put inside of a folder with a unique time stamp. Let's save our code and run it. When it's done running, let's go over to Google Drive and take a look at what happens.
As you can see, we now have a new MarkdownGen folder. And if we open it up, we have an output folder with a time stamp. Inside of the output folder is our new markdown file. Just like we had before. If we go back and run this again, we should now see a new folder has been created inside of our MarkdownGen folder. Here's the new time stamp folder. We open it up, and we'll get the markdown-test.md as well.
We can better organize this by selecting the list view and changing the order to last modified. As you can see, each new folder that's created will be shown on the top, so we always know which one is the latest file. Now we're able to create unique folders to store each of the generated files our script creates.
- Setting up Google Apps Script
- Setting up a document
- Working with files and folders
- Converting Google Docs to Markdown
- Traversing a Google Doc
- Parsing sections and elements
- Formatting text blocks
- Adding URLs
- Finding and saving images