How Can I Create a Sequential Series of Folders?

How Can I Create a Sequential Series of Folders?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I prompt a user to enter a starting number and ending number, then create a sequential series of folders based on those two numbers?

-- PM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PM. We had some time to kill this afternoon, so we thought we’d see what we could do about answering your question. In fact, if we didn’t try answering this question the Scripting Guy who writes this column would just be sitting around waiting until it was time to leave work and go to the movie No Reservations starring Catherine Zeta-Jones, a master chef who “…lives her life like she runs her kitchen at a trendy Manhattan eatery—with a no-nonsense intensity that both captivates and intimidates everyone around her.” As you might expect, Catherine’s “… perfectionist nature is put to the test when she ‘inherits’ her 9-year-old niece Zoe, while contending with a brash new sous-chef who joins her staff.”

Whew!

In case you’re wondering, no, this isn’t the kind of movie the Scripting Guy who writes this column would normally go to; as it is, the kind of movie the Scripting Guy who writes this column would normally go to is no movie whatsoever, seeing as how he maybe makes it out to one or two movies a year. (Compared to, say, 7 University of Washington football games, 20+ University of Washington basketball games, a handful of Seattle Mariners games, and several thousand Scripting Son baseball games.) So does that mean the Scripting Guy who writes this column is being forced to see this movie against his will? Yes, as a matter of fact, it – um, we mean no, no, of course not. He doesn’t just want to go to this movie; in fact, he’s simply dying to see what happens when rivalry becomes romance, and Catherine Zeta-Jones has to learn to express herself beyond the realm of her kitchen. Well, assuming that she wants to connect with Zoe and find true happiness with high-spirited and free-wheeling sous chef Nick Palmer, that is.

OK, so maybe we are being a bit facetious here; the movie reviews have been pretty good, and we expect this will be a perfectly enjoyable evening. Still, the Scripting Guy who writes this column would be happier if up-to-the-minute baseball scores continually scrolled across the bottom of the screen, the same way they do on ESPN.

At any rate, show time isn’t for another 6 hours or so, which leaves plenty of time to explain how to write a script that can prompt a user to enter a starting number and ending number, then create a sequential series of folders based on those two numbers:

Set objFSO = CreateObject("Scripting.FileSystemObject")

intStartingFolder = InputBox("Please enter the starting number:")
intEndingFolder = InputBox("Please enter the ending number:")

For i = intStartingFolder to intEndingFolder
    strNumber = i
    Do While Len(strNumber) < 5
        strNumber = "0" & strNumber
    Loop

    strFolder = "C:\Scripts\2007-" & strNumber
    Set objFolder = objFSO.CreateFolder(strFolder)
Next

Note. At this point, no, we have no plans to turn this folder creation script into a movie starring Catherine Zeta-Jones as a master scripter who “…lives her life like she runs her IT department at a trendy Redmond software company—with a no-nonsense intensity that both captivates and intimidates everyone around her.” But we haven’t ruled anything out, either. Catherine, if you’re reading this (and, like most Hollywood movie stars, we assume you are), give us a call.

While we wait for Catherine’s people to call our people, and while we wait until we actually get some people, let’s talk about the script. (The folder creation script, not the movie script.) As you can see, we start out by creating an instance of the Scripting.FileSystemObject object. No big surprise plot twist there; that’s the object of choice for creating new folders. Next we use the InputBox function to prompt the user to enter the starting folder number, a value we store in the variable intStartingFolder:

intStartingFolder = InputBox("Please enter the starting number:")

Following that, and just to add to the tension and suspense (will we ever find true happiness with high-spirited and free-wheeling folder creator Nick Palmer?), we call the InputBox function a second time, this time prompting the user to enter the ending folder number and storing that value in the variable named intEndingFolder:

intEndingFolder = InputBox("Please enter the ending number:")

Before we go any further we should note that we didn’t include any error-handling in this script. Could that create a problem? You bet it could. For one thing, there’s no way to cancel the script; even if the user clicks the Cancel button in the input box the script will continue to run. (And, needless to say, will fail.) Fortunately, that’s something we can correct by modifying our InputBox calls so they look like this:

intStartingFolder = InputBox("Please enter the starting number:")

If intStartingFolder = "" Then
    Wscript.Quit
End If

In other words, if the user clicks Cancel (meaning that the variable intStartingFolder gets assigned an empty string value) then we call the Quit method and terminate the script.

Much as someone should have done for the Michael Jordan “movie” Space Jam.

In addition, we should also include some code that verifies that the user actually entered a number and not a string value. For example, code similar to this:

If Not IsNumeric intStartingFolder Then
    Wscript.Echo "You must enter a number."
    Wscript.Quit
End If

Oh, and while we’re at it, we should also verify that the ending folder number is larger than the starting folder number:

If Not intEndingFolder <= intStartingFolder Then
    Wscript.Echo "The ending folder number must be larger than the starting folder number."
    Wscript.Quit
End If

However, that made the script a little too long and a little too complicated-looking so we left it all out. But don’t despair: the complete script will appear on the director’s cut DVD.

Once we have our starting and ending folder numbers our next task is to set up a For loop that runs from the starting number (say, 500) to the ending number (say, 519). That’s what this line of code is for:

For i = intStartingFolder to intEndingFolder

Inside this loop we assign the value of the counter variable i to the variable strNumber; in this example, that means that – the first time through the loop – strNumber will be equal to 500.

And now we’re going to take a slight detour. In PM’s original email the folders had names similar to this one, with five digits allocated for the folder number:

C:\Scripts\2007-00500

We’ve decided to adopt this same naming convention, which raises an important question: how can we ensure that our folders allocate five digits for the folder number? Well, to begin with, we use the Len function and the following line of code to determine whether the length of the value stored in strNumber (that is, the number of characters in the value) is less than 5:

Do While Len(strNumber) < 5

Let’s assume that it is, which is definitely true if strNumber equals 500. In that case, we use this line of code to set the value of strNumber to the existing value of strNumber plus a leading zero:

strNumber = "0" & strNumber

In our example, that means strNumber now equals 0500. We then loop around and check the length of the “new” strNumber variable. Because the length is 4 and 4 is less than 5, we add another leading zero; that makes strNumber equal to 00500. We then loop around and check the length again. Because the length is no longer less than 5 we break out of the Do While statement and get on with the show.

And what kind of excitement and adventure can we expect now that we’ve broken out of the Do While statement? Well, hang on to yours hats; for one thing, we’re going to use this line of code to construct a path for the new folder and store it in a variable named strFolder:

strFolder = "C:\Scripts\2007-" & strNumber

What’s strFolder going to be equal to? You got it: C:\Scripts\2007-00500, which just happens to be the path for one of the folders we need to create. With that in mind, we then call the CreateFolder method and create a new folder:

Set objFolder = objFSO.CreateFolder(strFolder)

At that point we loop around and the script automatically increments the value of our counter variable by 1 (making i equal to 501); we then we repeat the process. This continues until we’ve created the folder C:\Scripts\2007-00519, at which point we’ve connected with Zoe, found true happiness with Nick Palmer, and created a whole bunch of new folders to boot. If that doesn’t sound like a box office bonanza, well, we don’t know what does.

Anyway, we hope that answers your question, PM. It does? Oh. Well, that’s good, um, we were hoping that this would clear everything up. But listen, are you sure we answered your question; you know, did we completely answer this question for you? After all, if you have even the least bit of uncertainty about this issue then we’d be happy to explain it all over again, and as often as needed. In fact, we’re willing to do that even if we have to stay late, even if that means missing the movie that we’re so looking forward to seeing. Granted, that would be a considerable sacrifice on our part, but you know how it is for the Scripting Guys: business before pleasure. So just let us know if you have any questions, any questions at all. Any questions.

OK, not a single question, huh? All right; anyone else out there have a question about this script? Anyone? Hello? Hello?

Oh. OK. That’s … great ….

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This is just what I was looking for. My system did not like the statements with the "Not" operator, but they were not essential to the basic script. Saved me hours of drudgery and changed my mind about scripting altogether. Thanks!