{"id":64293,"date":"2007-08-07T01:18:00","date_gmt":"2007-08-07T01:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/07\/how-can-i-create-a-sequential-series-of-folders\/"},"modified":"2007-08-07T01:18:00","modified_gmt":"2007-08-07T01:18:00","slug":"how-can-i-create-a-sequential-series-of-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-sequential-series-of-folders\/","title":{"rendered":"How Can I Create a Sequential Series of Folders?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>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?<BR><BR>&#8212; PM <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, PM. We had some time to kill this afternoon, so we thought we\u2019d see what we could do about answering your question. In fact, if we <I>didn\u2019t<\/I> 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 <I>No Reservations<\/I> starring Catherine Zeta-Jones, a master chef who \u201c\u2026lives her life like she runs her kitchen at a trendy Manhattan eatery\u2014with a no-nonsense intensity that both captivates and intimidates everyone around her.\u201d As you might expect, Catherine\u2019s \u201c\u2026 perfectionist nature is put to the test when she \u2018inherits\u2019 her 9-year-old niece Zoe, while contending with a brash new sous-chef who joins her staff.\u201d <\/P>\n<P>Whew!<\/P>\n<P>In case you\u2019re wondering, no, this isn\u2019t 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 \u2013 um, we mean no, no, of course not. He doesn\u2019t just <I>want<\/I> to go to this movie; in fact, he\u2019s simply <I>dying<\/I> 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.<\/P>\n<P>OK, so maybe we <I>are<\/I> 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.<\/P>\n<P>At any rate, show time isn\u2019t 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:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>intStartingFolder = InputBox(&#8220;Please enter the starting number:&#8221;)\nintEndingFolder = InputBox(&#8220;Please enter the ending number:&#8221;)<\/p>\n<p>For i = intStartingFolder to intEndingFolder\n    strNumber = i\n    Do While Len(strNumber) &lt; 5\n        strNumber = &#8220;0&#8221; &amp; strNumber\n    Loop<\/p>\n<p>    strFolder = &#8220;C:\\Scripts\\2007-&#8221; &amp; strNumber\n    Set objFolder = objFSO.CreateFolder(strFolder)\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"ESD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. 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 \u201c\u2026lives her life like she runs her IT department at a trendy Redmond software company\u2014with a no-nonsense intensity that both captivates and intimidates everyone around her.\u201d But we haven\u2019t ruled anything out, either. Catherine, if you\u2019re reading this (and, like most Hollywood movie stars, we assume you are), give us a call.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>While we wait for Catherine\u2019s people to call our people, and while we wait until we actually <I>get<\/I> some people, let\u2019s 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 <B>Scripting.FileSystemObject<\/B> object. No big surprise plot twist there; that\u2019s the object of choice for creating new folders. Next we use the <B>InputBox<\/B> function to prompt the user to enter the starting folder number, a value we store in the variable intStartingFolder:<\/P><PRE class=\"codeSample\">intStartingFolder = InputBox(&#8220;Please enter the starting number:&#8221;)\n<\/PRE>\n<P>Following that, and just to add to the tension and suspense (will we <I>ever<\/I> 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 <I>that<\/I> value in the variable named intEndingFolder:<\/P><PRE class=\"codeSample\">intEndingFolder = InputBox(&#8220;Please enter the ending number:&#8221;)\n<\/PRE>\n<P>Before we go any further we should note that we didn\u2019t include any error-handling in this script. Could that create a problem? You bet it could. For one thing, there\u2019s no way to cancel the script; even if the user clicks the <B>Cancel<\/B> button in the input box the script will continue to run. (And, needless to say, will fail.) Fortunately, that\u2019s something we can correct by modifying our InputBox calls so they look like this:<\/P><PRE class=\"codeSample\">intStartingFolder = InputBox(&#8220;Please enter the starting number:&#8221;)<\/p>\n<p>If intStartingFolder = &#8220;&#8221; Then\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>In other words, if the user clicks <B>Cancel<\/B> (meaning that the variable intStartingFolder gets assigned an empty string value) then we call the <B>Quit<\/B> method and terminate the script.<\/P>\n<P>Much as someone should have done for the Michael Jordan \u201cmovie\u201d <I>Space Jam<\/I>.<\/P>\n<P>In addition, we should also include some code that verifies that the user actually entered a <I>number<\/I> and not a string value. For example, code similar to this:<\/P><PRE class=\"codeSample\">If Not IsNumeric intStartingFolder Then\n    Wscript.Echo &#8220;You must enter a number.&#8221;\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>Oh, and while we\u2019re at it, we should also verify that the ending folder number is larger than the starting folder number:<\/P><PRE class=\"codeSample\">If Not intEndingFolder &lt;= intStartingFolder Then\n    Wscript.Echo &#8220;The ending folder number must be larger than the starting folder number.&#8221;\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>However, that made the script a little too long and a little too complicated-looking so we left it all out. But don\u2019t despair: the complete script will appear on the director\u2019s cut DVD. <\/P>\n<P>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\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">For i = intStartingFolder to intEndingFolder\n<\/PRE>\n<P>Inside this loop we assign the value of the counter variable i to the variable strNumber; in this example, that means that \u2013 the first time through the loop \u2013 strNumber will be equal to 500. <\/P>\n<P>And now we\u2019re going to take a slight detour. In PM\u2019s original email the folders had names similar to this one, with five digits allocated for the folder number:<\/P><PRE class=\"codeSample\">C:\\Scripts\\2007-00500\n<\/PRE>\n<P>We\u2019ve decided to adopt this same naming convention, which raises an important question: how can we ensure that <I>our<\/I> folders allocate five digits for the folder number? Well, to begin with, we use the <B>Len<\/B> 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:<\/P><PRE class=\"codeSample\">Do While Len(strNumber) &lt; 5\n<\/PRE>\n<P>Let\u2019s 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 <I>plus<\/I> a leading zero:<\/P><PRE class=\"codeSample\">strNumber = &#8220;0&#8221; &amp; strNumber\n<\/PRE>\n<P>In our example, that means strNumber now equals 0500. We then loop around and check the length of the \u201cnew\u201d strNumber variable. Because the length is 4 and 4 is less than 5, we add <I>another<\/I> 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.<\/P>\n<P>And what kind of excitement and adventure can we expect now that we\u2019ve broken out of the Do While statement? Well, hang on to yours hats; for one thing, we\u2019re going to use this line of code to construct a path for the new folder and store it in a variable named strFolder:<\/P><PRE class=\"codeSample\">strFolder = &#8220;C:\\Scripts\\2007-&#8221; &amp; strNumber\n<\/PRE>\n<P>What\u2019s 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 <B>CreateFolder<\/B> method and create a new folder:<\/P><PRE class=\"codeSample\">Set objFolder = objFSO.CreateFolder(strFolder)\n<\/PRE>\n<P>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\u2019ve created the folder C:\\Scripts\\2007-00519, at which point we\u2019ve connected with Zoe, found true happiness with Nick Palmer, and created a whole bunch of new folders to boot. If that doesn\u2019t sound like a box office bonanza, well, we don\u2019t know what does.<\/P>\n<P>Anyway, we hope that answers your question, PM. It does? Oh. Well, that\u2019s good, um, we were <I>hoping<\/I> that this would clear everything up. But listen, are you <I>sure<\/I> we answered your question; you know, did we <I>completely<\/I> answer this question for you? After all, if you have even the <I>least bit<\/I> of uncertainty about this issue then we\u2019d be happy to explain it all over again, and as often as needed. In fact, we\u2019re willing to do that even if we have to stay late, even if that means missing the movie that we\u2019re so looking forward to seeing. Granted, that would be a <I>considerable<\/I> 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. <I>Any<\/I> questions.<\/P>\n<P>OK, not a single question, huh? All right; anyone else out there have a question about this script? Anyone? Hello? Hello?<\/P>\n<P>Oh. OK. That\u2019s \u2026 great \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?&#8212; PM Hey, PM. We had some time to kill this afternoon, so we thought we\u2019d see what we could do about answering your question. In fact, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,5],"class_list":["post-64293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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?&#8212; PM Hey, PM. We had some time to kill this afternoon, so we thought we\u2019d see what we could do about answering your question. In fact, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}