{"id":52423,"date":"2009-09-21T03:01:00","date_gmt":"2009-09-21T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/09\/21\/hey-scripting-guy-how-can-i-clean-up-file-names\/"},"modified":"2009-09-21T03:01:00","modified_gmt":"2009-09-21T03:01:00","slug":"hey-scripting-guy-how-can-i-clean-up-file-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-clean-up-file-names\/","title":{"rendered":"Hey, Scripting Guy! How Can I Clean Up File Names?"},"content":{"rendered":"<p><span class=\"sbmLink\">   <\/p>\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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;bookmark_title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;t=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;ti=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;title=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" 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\/21\/hey-scripting-guy-september-21-2009.aspx&amp;t=Hey, Scripting Guy! How Can I Clean Up File Names?\" target=\"_blank\"><img decoding=\"async\" border=\"0\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/yahoo9.png\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> <\/span>  <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\"><span><img decoding=\"async\" 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\" \/><\/span>Hey, Scripting Guy! We have a number of files that were manually created by various people in a common folder. Unfortunately, they did not follow a specific naming convention as well as they should, and as a result they do not sort very well. Some people preceded the file names with the word \u201cFile,\u201d and others appended their name to the file. In the middle are numbers that correspond to purchase order numbers. I would like to get rid of the word \u201cFile\u201d and the person&#8217;s name from the files and be left with only the purchase order numbers for the file names. There are hundreds of files in this folder, and it will take an entire weekend to clean this junk up. With football season starting up in the United States, I would much rather watch football and consume junk food than spend the weekend cooped up in an office cleaning up after clueless users.     <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">&#8212; JPC   <\/p>\n<p><font size=\"3\"><img decoding=\"async\" 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\" \/><\/font><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">Hello JPC, <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. You are right. Football season in the United States is a special time of the year. Because it is often accompanied by the transition from hot summer months to more temperate fall weather, it is a special time of the year. Or maybe it is just the marching bands and their half time shows that make things interesting. In <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte,_North_Carolina\"><font face=\"Segoe\">Charlotte, North Carolina<\/font><\/a>, we have a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Carolina_Panthers\"><font face=\"Segoe\">professional football team<\/font><\/a> as well as several college and university teams we can watch. It makes things interesting on the weekend if you are trying to consume as much football and junk food as possible. Speaking of interesting, a similar question was asked a few years ago by RE when RE wanted to know <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2007\/07\/20\/how-can-i-delete-specified-characters-from-the-beginning-and-end-of-my-file-names.aspx\"><font face=\"Segoe\">how to delete specified characters from the beginning and end of file names<\/font><\/a>. In that article, a pretty cool VBScript was created. It is seen here.    <\/p>\n<p style=\"margin: 15pt 12pt 0pt 0in\" class=\"CodeBlockScreenedHead\"><strong>DeleteCharactersFromBeginningAndEndOfFileNames.vbs     <\/p>\n<p>   <\/strong><\/p>\n<p style=\"margin: 15pt 17.3pt 0.25in 0in\" class=\"CodeBlockScreened\"><span style=\"font-size: 9.5pt\" lang=\"EN\"><font style=\"background-color: #f2f2f2\"><font face=\"Lucida Sans Typewriter\">strComputer = &quot;.&quot;         <\/p>\n<p>Set objWMIService = GetObject(&quot;winmgmts:\\\\&quot; &amp; strComputer &amp; &quot;\\root\\cimv2&quot;)          <\/p>\n<p>Set colFiles = objWMIService.ExecQuery _          <br \/><span>&#160;&#160;&#160; <\/span>(&quot;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Test&#8217;} Where &quot; _          <br \/><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span>&amp; &quot;ResultClass = CIM_DataFile&quot;)          <\/p>\n<p>For Each objFile In colFiles          <br \/><span>&#160;&#160;&#160; <\/span>strPath = objFile.Drive &amp; objFile.Path          <br \/><span>&#160;&#160;&#160; <\/span>strExtension = objFile.Extension          <br \/><span>&#160;&#160;&#160; <\/span>strFileName = objFile.FileName          <\/p>\n<p><span>&#160;&#160;&#160; <\/span>If Left(strFileName, 5) = &quot;File &quot; Then          <br \/><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span>intLength = Len(strFileName)          <br \/><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span>strFileName = Right(strFileName, intLength &#8211; 5)          <br \/><span>&#160;&#160;&#160; <\/span>End If          <\/p>\n<p><span>&#160;&#160;&#160; <\/span>If Right(strFileName, 7) = &quot; George&quot; Then          <br \/><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span>intLength = Len(strFileName)          <br \/><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span>strFileName = Left(strFileName, intLength &#8211; 7)          <br \/><span>&#160;&#160;&#160; <\/span>End If          <\/p>\n<p><span>&#160;&#160;&#160; <\/span>strNewName = strPath &amp; strFileName &amp; &quot;.&quot; &amp; strExtension          <br \/><span>&#160;&#160;&#160; <\/span>errResult = objFile.Rename(strNewName)          <br \/>Next          <\/p>\n<p>       <\/font><\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">JPC, a Windows PowerShell script could be produced that would use the <b>Associators Of<\/b> query from WMI and could mimic rather closely the syntax of the VBScript. But this script points out the <a href=\"http:\/\/blogs.msdn.com\/microsoft_press\/archive\/2009\/09\/14\/ed-wilson-on-translating-vbscript-to-windows-powershell.aspx\"><font face=\"Segoe\">danger of blindly translating VBScript code into Windows PowerShell code<\/font><\/a> because in Windows PowerShell there is a much better way of performing this task than using the rather complicated <b>Associators Of<\/b> WMI query. For more information about translating <b>Associators Of<\/b> WMI queries from VBScript to Windows PowerShell (and why you might need to do such a thing), see the <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/03\/04\/how-do-i-migrate-my-vbscript-wmi-queries-to-windows-powershell.aspx\"><font face=\"Segoe\">How Do I Migrate My VBScript Queries to Windows PowerShell?<\/font><\/a> article.     <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">I actually wrote two Windows PowerShell scripts that will delete words from the beginning and end of the file names that are stored in the folder. The folder containing the target files is seen here:    <\/p>\n<p style=\"margin: 9pt 0in 4pt\" class=\"Fig-Graphic\"><img decoding=\"async\" style=\"width: 600px;height: 450px\" title=\"Image of folder containing target files\" alt=\"Image of folder containing target files\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0921\/hsg-09-21-09-01.jpg\" width=\"600\" height=\"450\" \/>    <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">Both scripts use the <b>replace<\/b> operator to replace the strings with an empty space. The first script is called DeleteCharactersFromBeginningAndEndOfFileName.ps1 and is shown here.     <\/p>\n<p style=\"margin: 15pt 12pt 0pt 0in\" class=\"CodeBlockScreenedHead\"><strong>DeleteCharactersFromBeginningAndEndOfFileName.ps1     <\/p>\n<p>   <\/strong><\/p>\n<p style=\"margin: 15pt 17.3pt 0.25in 0in\" class=\"CodeBlockScreened\"><span style=\"font-size: 9.5pt\"><font style=\"background-color: #f2f2f2\"><font face=\"Lucida Sans Typewriter\">Get-ChildItem -path c:\\fso -Filter *.txt |         <br \/>ForEach-Object {          <br \/><span>&#160;<\/span>$name = $_.name -replace &quot;file &quot;,&quot;&quot;          <br \/><span>&#160;<\/span>$name = $name -replace &quot; george&quot;,&quot;&quot;          <br \/><span>&#160;<\/span>Rename-Item -Path $_.fullname -NewName $name          <br \/>}          <br \/>Get-ChildItem -path c:\\fso -Filter *.txt          <\/p>\n<p>       <\/font><\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">In the DeleteCharactersFromBeginningAndEndOfFileName.ps1 script, the first thing that is done is to retrieve all of the text files. Because text files have a .txt extension, the <b>\u2013filter<\/b> parameter is used to limit the search results that are returned by the <b>Get-ChildItem<\/b> cmdlet. The <b>\u2013path<\/b> parameter tells the <b>Get-ChildItem<\/b> cmdlet where to begin looking for all the text files. The results of this command are piped to the <b>ForEach-Object<\/b> cmdlet. This is seen here:     <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">Get-ChildItem -path c:\\fso -Filter *.txt |       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">The <b>ForEach-Object<\/b> cmdlet is used to allow Windows PowerShell to work with each file as it comes across the pipeline. The first thing that is done is to replace the word \u201cfile\u201d<i> <\/i>with an empty string. The <b>replace<\/b> operator is used to do pattern matching on a string. The <b>name<\/b> property from the System.IO.FileInfo class returns a string, and therefore the <b>tostring<\/b> method is not required to convert the name to a string. The <b>replace<\/b> operator takes two parameters: the first is the pattern to be matched, and the second is the value to replace the matched pattern with. This is illustrated here:     <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $a = Get-Item C:\\fso\\a.txt       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $a.Name       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">a.txt       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $a.Name<span>&#160; <\/span>-replace &quot;a&quot;,&quot;b&quot;        <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">b.txt       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">After the word \u201cfile\u201d has been replaced, the resulting string is stored in the variable <b>$name<\/b>. This is seen here:     <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">$name = $_.name -replace &quot;file &quot;,&quot;&quot;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">This line of code takes care of the problem of the word \u201cfile\u201d when it is found at the beginning of the string. Now you need to replace the word \u201cGeorge,\u201d which occurs at the end of some of the strings. To do this, use the string stored in the <b>$name<\/b> variable, and the <b>replace<\/b> operator to clean up the end of the file names. This is seen here:     <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">$name = $name -replace &quot; george&quot;,&quot;&quot;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">After the second <b>replace<\/b> operation has completed, the new file name is used in a call to the <b>Rename-Item<\/b> cmdlet. When using the <b>Rename-Item<\/b> cmdlet, the first parameter is the <b>path<\/b> parameter that must give the complete location to the file. The second parameter is the new name for the file. You cannot use the <b>Rename-Item<\/b> cmdlet to rename and move a file to a new location. To move a file, you must use the <b>Move-Item<\/b> cmdlet. The renaming of the file is seen here:    <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">Rename-Item -Path $_.fullname -NewName $name       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">After all of the files have been renamed, the <b>Get-ChildItem<\/b> cmdlet is used to produce a listing of the folder to ensure the file names have been changed. This is seen here:     <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">Get-ChildItem -path c:\\fso -Filter *.txt       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 9pt 0in 4pt\" class=\"Fig-Graphic\">The renamed files are seen in this image:   <\/p>\n<p style=\"margin: 9pt 0in 4pt\" class=\"Fig-Graphic\"><img decoding=\"async\" style=\"width: 600px;height: 263px\" title=\"Image of the renamed files\" alt=\"Image of the renamed files\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0921\/hsg-09-21-09-02.jpg\" width=\"600\" height=\"263\" \/>    <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">The second script is a little more efficient and a bit shorter, but is also more complicated to read. The change to the script involves using one <b>replace<\/b> command instead of two. For this scenario, the results of the two scripts are exactly the same, but the two <b>replace<\/b> operations are not the same. The second form is more flexible because it will remove everything that is not a number from the file name. To do this, it uses the regular expression pattern <b>\\D<\/b> that matches everything that is not a number.     <\/p>\n<blockquote>\n<p style=\"margin: 15pt 12pt 0.25in 0in\" class=\"Readeraidonly\">For more information on using regular expressions from within Windows PowerShell, you can refer to <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/tags\/regular+expressions\/Windows+PowerShell\/default.aspx\"><font face=\"Segoe\">this collection of Hey Scripting Guy! articles<\/font><\/a>. Pay particular attention to the ones from the week of April 13, 2009, because that was Regular Expression Week on the TechNet Script Center.       <\/p>\n<\/blockquote>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">The use of the <strong>\\D<\/strong> regular expression pattern is shown here:    <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $filz = &quot;file 123 ed.txt&quot;,&quot;fyle 145556 bob.txt&quot;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $filz | foreach-object { $_ -replace &quot;\\D&quot;,&quot;&quot; }       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">123       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">145556       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">PS C:\\&gt;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">The modified line of code from the RegexRemoveCharactersFromFileName.ps1 script is seen here:    <\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">$name = ($_.name -replace &quot;\\D&quot;,&quot;&quot;) + &quot;.txt&quot;       <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">The complete RegexRemoveCharactersFromFileName.ps1 script is seen here:    <\/p>\n<p style=\"margin: 15pt 12pt 0pt 0in\" class=\"CodeBlockScreenedHead\"><strong>RegexRemoveCharactersFromFileName.ps1     <\/p>\n<p>   <\/strong><\/p>\n<p style=\"margin: 4pt 0in 7pt;background: #f2f2f2\" class=\"CodeBlock\"><span style=\"font-size: 9.5pt\"><font face=\"Lucida Sans Typewriter\">Get-ChildItem -path c:\\fso -Filter *.txt |       <br \/>ForEach-Object {        <br \/><span>&#160;<\/span>$name = ($_.name -replace &quot;\\D&quot;,&quot;&quot;) + &quot;.txt&quot;        <br \/><span>&#160;<\/span>Rename-Item -Path $_.fullname -NewName $name        <br \/>}        <br \/>Get-ChildItem -path c:\\fso -Filter *.txt        <\/p>\n<p>     <\/font><\/span><\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">&#160;<\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">Well, JPC, that is about all there is to renaming folders that are named after a specific pattern. If you want to know exactly what we will be covering tomorrow, follow us on <a title=\"Twitter\" href=\"http:\/\/www.twitter.com\/scriptingguys\/\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a title=\"Facebook\" href=\"http:\/\/bit.ly\/8u4wU\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.     <\/p>\n<p style=\"margin: 0in 0in 8pt\" class=\"MsoNormal\">\n<p>&#160;<\/p>\n<\/p>\n<p style=\"margin-left: 0in\"><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys       <\/p>\n<p>     <\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Share this post: Hey, Scripting Guy! We have a number of files that were manually created by various people in a common folder. Unfortunately, they did not follow a specific naming convention as well as they should, and as a result they do not sort very well. Some people preceded the file names with the [&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,174,3,4,12,21,45],"class_list":["post-52423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Share this post: Hey, Scripting Guy! We have a number of files that were manually created by various people in a common folder. Unfortunately, they did not follow a specific naming convention as well as they should, and as a result they do not sort very well. Some people preceded the file names with the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52423","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=52423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52423\/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=52423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}