{"id":52503,"date":"2009-09-09T03:01:00","date_gmt":"2009-09-09T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/09\/09\/hey-scripting-guy-can-i-create-new-folders-in-a-sequentially-numbered-series\/"},"modified":"2009-09-09T03:01:00","modified_gmt":"2009-09-09T03:01:00","slug":"hey-scripting-guy-can-i-create-new-folders-in-a-sequentially-numbered-series","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-create-new-folders-in-a-sequentially-numbered-series\/","title":{"rendered":"Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?"},"content":{"rendered":"<p><SPAN class=\"sbmLink\">\n<TABLE cellSpacing=\"1\" cellPadding=\"1\">\n<TBODY>\n<TR>\n<TD class=\"sbmText\">Share this post: <\/TD>\n<TD><A title=\"Post it to Backflip!\" href=\"http:\/\/www.backflip.com\/add_page_pop.ihtml?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/backflip4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Buddymark!\" href=\"http:\/\/buddymarks.com\/s_add_bookmark.php?bookmark_url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;bookmark_title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/buddymar4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Del.icio.us!\" href=\"http:\/\/del.icio.us\/post?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/deliciou4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Digg!\" href=\"http:\/\/digg.com\/submit?phase=2&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/digg14.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Facebook!\" href=\"http:\/\/www.facebook.com\/sharer.php?u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;t=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/telligent.evolution.components.attachments\/13\/4982\/00\/00\/01\/76\/53\/19\/facebook.JPG\"><\/A><\/TD>\n<TD><A title=\"Post it to Furl!\" href=\"http:\/\/www.furl.net\/store?s=f&amp;to=0&amp;u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;ti=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/furl4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Netvouz!\" href=\"http:\/\/netvouz.com\/action\/submitBookmark?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/netvouz4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Reddit!\" href=\"http:\/\/reddit.com\/submit?url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/reddit4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Spurl!\" href=\"http:\/\/www.spurl.net\/spurl.php?v=3&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/spurl8.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Technorati!\" href=\"http:\/\/technorati.com\/faves\/?add=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/technora4.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Wists!\" href=\"http:\/\/www.wists.com\/?action=add&amp;url=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;title=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wists9.png\"><\/A><\/TD>\n<TD><A title=\"Post it to Yahoo!\" href=\"http:\/\/myweb.yahoo.com\/myresults\/bookmarklet?u=http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/09\/09\/hey-scripting-guy-september-9-2009.aspx&amp;t=Hey, Scripting Guy! Can I Create New Folders in a Sequentially Numbered Series?\" target=\"_blank\"><IMG border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/yahoo9.png\"><\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/SPAN>\n<P class=\"MsoNormal\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/P>\n<P class=\"MsoNormal\">Hey, Scripting Guy! I hate to type numbers. I actually took typing in high school and did really well with it. I was able to type more than 40 words per minute at the end of the class with no errors. I got an A for the class. But, I was horrible at doing numbers. Maybe it was because we did not practice them as often or maybe it is because I hated math when I was in school. For whatever reason, I think I would rather go to the doctor\u2019s office and give blood than to have to type a document with a bunch of numbers in it. \n<P class=\"MsoNormal\">This background information is intended help you put my request in perspective. We use folders that are set up with the day of year number. Inside each folder numbered with the day of the year, we create a new folder with the day of the year and the two digits of the year appended to it. The folders are in a modified <A href=\"http:\/\/en.wikipedia.org\/wiki\/Ordinal_date\"><FONT face=\"Segoe\">ordinal date<\/FONT><\/A> format. Needless to say, such a prospect just drives me crazy. Can you write a script that will create a folder that is the next number in the series and append the two year date to it? \n<P class=\"MsoNormal\">&#8212; SC \n<P><SPAN><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><\/SPAN><\/P>\n<P class=\"MsoNormal\">Hello SC, <\/P>\n<P class=\"MsoNormal\">I can sympathize with your aversion, especially because you say you would rather go to the doctor\u2019s office and give blood. Personally, I am allergic to needles, and I break out in a cold sweat when I have to go see the lab person. I would much rather go to San Francisco, California, and head down to <A href=\"http:\/\/en.wikipedia.org\/wiki\/Fisherman%27s_Wharf,_San_Francisco,_California\"><FONT face=\"Segoe\">Fisherman&#8217;s Wharf<\/FONT><\/A> and watch the California sea lions wrestle: \n<P class=\"Fig-Graphic\"><IMG title=\"Image of California sea lions\" alt=\"Image of California sea lions\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0909\/hsg-09-09-09-01.jpg\" width=\"600\" height=\"450\">&nbsp; \n<P class=\"Readeraidonly\">This week we are looking at questions that have been posted to the <A href=\"http:\/\/bit.ly\/2h50Tk\"><FONT face=\"Segoe\">Official Scripting Guys Forum<\/FONT><\/A>. This user forum is a great place to ask questions related to VBScript or Windows PowerShell. It is also an excellent resource to learning scripting by either reading answers to others questions, or by becoming involved in discussions through posting answers. In addition to being a learning resource the forum is also a fun place to interact with people from around the world who have an interest in scripting. The Official Scripting Guys Forum is free, and you can read all questions and answers by going to the web site. If you wish to post a question or propose an answer to a question you will need to log in with your <A href=\"http:\/\/bit.ly\/wI4Nj\"><FONT face=\"Segoe\">Windows Live ID<\/FONT><\/A>. \n<P class=\"MsoNormal\">SC, I was recently trolling through the Official Scripting Guys Forum, and I ran across a similar question to yours. The good thing is that Brandon, one of our moderators on the Official Scripting Guys Forum, and a Microsoft MVP for Windows PowerShell had already posted a cleaver answer to the question. This is seen here: \n<P class=\"Fig-Graphic\"><IMG title=\"Image of question on forum\" alt=\"Image of question on forum\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0909\/hsg-09-09-09-02.jpg\" width=\"600\" height=\"429\">&nbsp; \n<P class=\"MsoNormal\"><BR>After reading Brandon&#8217;s answer to the previous question, I decided to play around with his answer, and add some error checking and other things that would make it a cool script for you. The CreateNewFolderWithPattern.ps1 script is seen here. \n<P class=\"CodeBlockScreenedHead\"><STRONG>CreateNewFolderWithPattern.ps1 \n<P><\/STRONG><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">Param($FolderPath) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">Function Test-FolderPath($folderPath) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">{ <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if(-not(Test-Path -path $folderPath)) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp; <\/SPAN>{ <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>New-Item -path $folderPath -ItemType Directory -force | <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>Out-Null <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp; <\/SPAN>} <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">} #end function Test-FolderPath <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">Function New-CustomFolder($folderPath) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">{ <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Parent = Split-Path $FolderPath -Parent <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Leaf = Split-Path $FolderPath -leaf <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if($leaf -notmatch &#8220;\\d&#8221;) { &#8220;Folder name must be a number. Such as 55.12 or 12356&#8221; ; exit } <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Folder = Get-item $FolderPath <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$DefaultName = &#8220;{0}\\{1}.{2}&#8221; -f $Parent,([int]($Folder.Name.split(&#8220;.&#8221;)[0])+1),(get-date -f yy) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$NewFolder = Read-Host &#8220;Name of New Folder? [Default] $DefaultName&#8221; <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if(!$NewFolder) <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>{ <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp; <\/SPAN>$NewFolder = $DefaultName <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>} <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if(-not(Test-Path -path $NewFolder)) <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp; <\/SPAN>{ New-Item -ItemType Directory -path $NewFolder } <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>Else { &#8220;$NewFolder already exists.&#8221; ; exit } <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">} #end function New-CustomFolder <\/FONT><\/FONT><\/SPAN><SPAN><\/P><\/SPAN>\n<P class=\"CodeBlockScreened\"><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\"># *** Entry Point to Script *** \n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">Test-FolderPath -folderPath $folderPath <\/P>\n<P><\/FONT><\/FONT><\/SPAN><SPAN><FONT><FONT face=\"Lucida Sans Typewriter\">New-CustomFolder -folderPath $folderPath <\/P><\/FONT><\/FONT><\/SPAN>\n<P class=\"MsoNormal\">The CreateNewFolderWithPattern.ps1 script begins by creating a command-line parameter. The parameter to be supplied to the script is the path to the parent folder. Because of the unique requirements of this solution, the child folder name must contain only numbers. The <B>param<\/B> statement is used to create the command-line parameter. This is seen here: \n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">Param($FolderPath) \n<P><\/FONT><\/SPAN>The first function that is seen in the CreateNewFolderWithPattern.ps1 script is the <B>Test-FolderPath<\/B> function. This function uses the <B>Test-Path<\/B> cmdlet to determine if the folder that was supplied via the command line exists on the system. If the folder does not exist, a new folder is created. The <B>force<\/B> parameter is used to tell Windows PowerShell to create child paths even if the parent paths do not exist. The result of the operation is piped to the <B>Out-Null<\/B> cmdlet to keep from cluttering the Windows PowerShell console. This is seen here: <\/P><SPAN><FONT face=\"Lucida Sans Typewriter\">\n<P class=\"CodeBlock\"><SPAN>Function Test-FolderPath($folderPath)<\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN>{<\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN><SPAN>&nbsp;<\/SPAN>if(-not(Test-Path -path $folderPath)) <\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN><SPAN>&nbsp;&nbsp; <\/SPAN>{ <\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>New-Item -path $folderPath -ItemType Directory -force |<\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>Out-Null<\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN><SPAN>&nbsp;&nbsp; <\/SPAN>}<\/SPAN><\/P>\n<P class=\"CodeBlock\"><SPAN>} #end function Test-FolderPath<\/SPAN><\/P>\n<P><BR><FONT face=\"arial,helvetica,sans-serif\">After the <B>Test-FolderPath<\/B> function has been created, you turn your attention to the <B>New-CustomFolder<\/B> function. This function first uses the <B>Split-Path<\/B> cmdlet to retrieve the parent portion of the path. It next uses the <B>Split-Path<\/B> function with the <B>leaf<\/B> parameter to retrieve the child portion of the path name. The last portion of a path to a folder is always the leaf portion. Everything else is the parent portion of the path. Some examples are seen in <B>Table 1<\/B>. <BR><BR><\/FONT>\n<P class=\"MsoNormal\"><STRONG><FONT face=\"arial,helvetica,sans-serif\">Table 1<SPAN>&nbsp; <\/SPAN>Parent and Leaf Examples<\/FONT><\/STRONG> <\/P>\n<TABLE class=\"MsoNormalTable\" border=\"1\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD vAlign=\"top\" width=\"173\">\n<P class=\"TableHead\"><B>Path \n<P>&nbsp;<\/P><\/B><\/TD>\n<TD vAlign=\"top\" width=\"151\">\n<P class=\"TableHead\"><B>Parent \n<P>&nbsp;<\/P><\/B><\/TD>\n<TD vAlign=\"top\" width=\"156\">\n<P class=\"TableHead\"><B>Leaf \n<P>&nbsp;<\/P><\/B><\/TD><\/TR>\n<TR>\n<TD vAlign=\"top\" width=\"173\">\n<P class=\"TableText\">C:\\fso \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"151\">\n<P class=\"TableText\">C:\\ \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"156\">\n<P class=\"TableText\">Fso \n<P>&nbsp;<\/P><\/TD><\/TR>\n<TR>\n<TD vAlign=\"top\" width=\"173\">\n<P class=\"TableText\">C:\\fso\\fso1 \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"151\">\n<P class=\"TableText\">C:\\fso \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"156\">\n<P class=\"TableText\">Fso1 \n<P>&nbsp;<\/P><\/TD><\/TR>\n<TR>\n<TD vAlign=\"top\" width=\"173\">\n<P class=\"TableText\">C:\\fso\\fso1\\fso2 \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"151\">\n<P class=\"TableText\">C:\\fso\\fso1 \n<P>&nbsp;<\/P><\/TD>\n<TD vAlign=\"top\" width=\"156\">\n<P class=\"TableText\">Fso2 \n<P>&nbsp;<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P class=\"MsoNormal\"><BR><FONT face=\"arial,helvetica,sans-serif\">The two <B>Split-Path<\/B> commands are seen here: <\/FONT>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">Function New-CustomFolder($folderPath) <\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">{<\/FONT><\/SPAN> \n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">$Parent = Split-Path $FolderPath -Parent <\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Leaf = Split-Path $FolderPath -leaf <\/FONT><\/SPAN>\n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">The script expects the leaf (child path) to be a number. But because the way the path is passed into the parameter, the leaf would always be considered to be a string. This prevents using the <B>is<\/B> operator to see if the leaf is a string or if it is an integer. The use of the <B>is<\/B> operator is seen here:<\/FONT> <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; 5 -is [int] <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">True <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; &#8220;five&#8221; -is [string] <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">True <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; \n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">After playing around with a few different ideas, I finally decided to use a regular expression pattern match (<B>&#8220;\\d&#8221;<\/B>) to see if the leaf matched a number. If it does not match a number, the leaf is a string and will cause an error when we later attempt to do an <B>op<\/B> addition. A message stating that the folder name must be a number is displayed and the script exits. This is seen here:<\/FONT> <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if($leaf -notmatch &#8220;\\d&#8221;) { &#8220;Folder name must be a number. Such as 55.12 or 12356&#8221; ; exit } <BR><\/P><\/FONT><\/SPAN>\n<P class=\"MsoNormal\"><BR><FONT face=\"arial,helvetica,sans-serif\">The <B>Get-Item<\/B> cmdlet is used to retrieve the folder specified by the <B>$FolderPath<\/B> variable. After the <B>folder<\/B> object is retrieved and stored in the <B>$Folder<\/B> variable, the new folder name is generated. The new folder name is created by using the <B>format<\/B> operator, which accepts a pattern for replacement values. In this example, three replacement items are used: the parent folder path, the name of the leaf folder that is converted into an integer and incremented by one, and a two-digit representation of the current year. <\/FONT>\n<P class=\"MsoNormal\"><BR><FONT face=\"arial,helvetica,sans-serif\">When using the <B>format<\/B> operator, each value that will be substituted goes inside a pair of curly brackets. The first item to be substituted is <B>{0}<\/B> and the second one is <B>{1}<\/B>. The replacement pattern is placed inside a pair of quotation marks. The replacement values are on the right side of the format operator, and these are separated by commas. A simple example consists of a replacement pattern that comprises two items. The two items are separated by a space <B>&#8220;{0} {1}&#8221;<\/B> followed by the closing quotation mark. The format operator <B>\u2013f<\/B> is followed by the two replacement values, <B>Hello<\/B> and <B>Ed<\/B>. When the line of code is executed, the string <B>Hello Ed<\/B> is returned to the console. This is seen here:<BR><\/FONT>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; &#8220;{0} {1}&#8221; -f &#8220;Hello&#8221;,&#8221;Ed&#8221; <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">Hello Ed <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">PS C:\\&gt; \n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">The use of replacement patterns can simplify some of the concatenation that might otherwise be required. It is an elegant solution to a common problem. This is seen here:<\/FONT> <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$Folder = Get-item $FolderPath <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$DefaultName = &#8220;{0}\\{1}.{2}&#8221; -f $Parent,([int]($Folder.Name.split(&#8220;.&#8221;)[0])+1),(get-date -f yy) \n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">The <B>Read-Host<\/B> cmdlet is used to prompt the user to use the newly created folder name. If the user chooses to accept the newly created folder name that is stored in the <B>$newFolder<\/B> variable, it will be assigned to the value of the <B>$DefaultName<\/B> variable. This is seen here:<\/FONT> <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>$NewFolder = Read-Host &#8220;Name of New Folder? [Default] $DefaultName&#8221; <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if(!$NewFolder) <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>{ <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp; <\/SPAN>$NewFolder = $DefaultName <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>} \n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">If the script has been run previously, it is possible the folder specified by the <B>$NewFolder<\/B> path might already exist. To forestall this problem, the <B>Test-Path<\/B> cmdlet is used as seen here. After it has been determined the folder does not exist, the <B>New-Item <\/B>cmdlet is used to create a new folder. If the folder does exist, a message stating the folder exists is displayed. This is seen here: <\/FONT><\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>if(-not(Test-Path -path $NewFolder)) <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;&nbsp; <\/SPAN>{ New-Item -ItemType Directory -path $NewFolder } <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\"><SPAN>&nbsp;<\/SPAN>Else { &#8220;$NewFolder already exists.&#8221; ; exit } <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">} #end function New-CustomFolder \n<P><\/FONT><\/SPAN><FONT face=\"arial,helvetica,sans-serif\">The entry point of the script calls the <B>Test-FolderPath<\/B> function to ensure that the parent folder exists. After that is determined, the <B>New-CustomFolder<\/B> function is called to create the new folder. This is seen here:<\/FONT> <\/P>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">Test-FolderPath -folderPath $folderPath <\/P><\/FONT><\/SPAN>\n<P class=\"CodeBlock\"><SPAN><FONT face=\"Lucida Sans Typewriter\">New-CustomFolder -folderPath $folderPath \n<P><\/FONT><\/SPAN><BR><FONT face=\"arial,helvetica,sans-serif\">SC, thank you for your question you sent to <\/FONT><A href=\"mailto:scripter@microsoft.com\"><FONT face=\"arial,helvetica,sans-serif\">scripter@microsoft.com<\/FONT><\/A><FONT face=\"arial,helvetica,sans-serif\">. It was a great question, especially because it had already been answered in the Official Scripting Guys Forum. Join us tomorrow as Script Forum Week continues.<\/FONT><\/P>\n<P class=\"MsoNormal\"><FONT face=\"arial,helvetica,sans-serif\">If you want to know exactly what we will be covering tomorrow, follow us on <\/FONT><A href=\"http:\/\/www.twitter.com\/scriptingguys\/\"><FONT face=\"arial,helvetica,sans-serif\">Twitter<\/FONT><\/A><FONT face=\"arial,helvetica,sans-serif\"> or <\/FONT><A href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\"><FONT face=\"arial,helvetica,sans-serif\">Facebook<\/FONT><\/A><FONT face=\"arial,helvetica,sans-serif\">. If you have any questions, send us e-mail at <\/FONT><A href=\"mailto:scripter@microsoft.com\"><FONT face=\"arial,helvetica,sans-serif\">scripter@microsoft.com<\/FONT><\/A><FONT face=\"arial,helvetica,sans-serif\"> or post them to the <\/FONT><A href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\"><FONT face=\"arial,helvetica,sans-serif\">Official Scripting Guys Forum<\/FONT><\/A><FONT face=\"arial,helvetica,sans-serif\">. See you tomorrow. Until then, keep on scripting.<\/FONT>&nbsp; \n<P><B><SPAN>Ed Wilson and Craig Liebendorfer, Scripting Guys \n<P>&nbsp;<\/P><\/SPAN><\/B><\/FONT><\/SPAN><FONT face=\"Lucida Sans Typewriter\"><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Share this post: Hey, Scripting Guy! I hate to type numbers. I actually took typing in high school and did really well with it. I was able to type more than 40 words per minute at the end of the class with no errors. I got an A for the class. But, I was horrible [&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":[38,11,174,208,3,4,12,45],"class_list":["post-52503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-regular-expressions","tag-script-center","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Share this post: Hey, Scripting Guy! I hate to type numbers. I actually took typing in high school and did really well with it. I was able to type more than 40 words per minute at the end of the class with no errors. I got an A for the class. But, I was horrible [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52503","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=52503"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52503\/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=52503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}