![]() |
||||||
revJournal - Revolution TutorialsHandy Handlers #6: EnsurePath
When writing to a data file using the "open" or "put" commands, Revolution will create the file for you if it doesn't already exist. But what about the folder the file will be created in? To ensue that the folder will be there when you need it, just run the path though this function and it'll take care of the rest. Using the "there is" and "there is not" operators Revolution provides a built-in operator for checking the existence of just about anything, from controls and stacks to files and folders. In keeping with Revolution's English-like flavor, this operator is simply called "there is", and using it is a snap:
We can modify that snippet to check for the folder first easily enough:
Adding Error-Checking We'll add an error-check after the "create folder" command, checking the value of "the result" which will contain an error message if there was a problem which prevented the folder from being created, or will be empty if not:
So we'll append our error-check to include the folder path:
We could copy and paste the code each time we use it, but that's not merely inefficient, more importantly it isn't fun. Let's turn that into its own separate function instead, so we can call it from anywhere. Turning the Error-Checking into a Separate Function Of course the first thing we have to do is decide on a name for this function. Since it ensures that a folder path will be available, we can call it "fwEnsurePath". The "fw" prefix helps you remember where you got this handler from, and better ensures that it'll avoid conflict with any new tokens added to the Revolution engine in the future, or functions in other libraries you might use. For more on this and other naming conventions see "Scripting Style Guide". This function will need only one argument, the path we're ensuring, so we'll be able to call it like this:
This gives us a one-line way to ensure that any path we want to write to will be there. Having defined how we want to use it, we can now get down to writing the function itself. Since we want it to be able to handle any path, which may refer to any number of subfolders, we'll need to walk through each part of the path one directory at a time, checking each folder along the way and creating it as needed, or reporting an error if the folder couldn't be created. To handle any number of folders in the path we'll use a repeat loop, and we can parse the string of paths easily if we first set the itemdelimiter to the character Rev uses to delimit folders in paths, "/". Within the repeat loop we'll build a copy of the path in a variable named tTestPath, adding a new folder leaf and checking it each time through the loop. By the time we're done each folder will have been checked, and if no error was reported the path passed to it will be ensured. Here's how these elements come together in the completed function:
As the series progresses you'll find more of the code in this articles making similar use of commands and functions we've already written. For example, in the next article you'll see how we put fwEnsurePath to work in creating a simple pair of handlers for writing and reading preferences files. About the Author Copyright 2003-2007 Fourth World Media Corporation. All rights reserved. This article is licensed for publication at revJournal.com. Any executable code in this article can be used freely without restriction, but the descriptive text of the article cannot be published in whole or in part without permission from the author. |
||