{"id":66673,"date":"2006-08-16T07:29:00","date_gmt":"2006-08-16T07:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/16\/how-can-i-remove-blank-spaces-from-file-names\/"},"modified":"2006-08-16T07:29:00","modified_gmt":"2006-08-16T07:29:00","slug":"how-can-i-remove-blank-spaces-from-file-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-blank-spaces-from-file-names\/","title":{"rendered":"How Can I Remove Blank Spaces From File Names?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! I have a folder with a bunch of files in it, files with names like Doc 1.pdf, Doc 2.pdf, and Doc 3.pdf. How do I remove the blank spaces from all those file names?<BR><BR>&#8212; JRP<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JRP. You know, today\u2019s column represents a landmark in <I>Hey, Scripting Guy!<\/I> history. Why\u2019s that? Because the Scripting Guy who writes this column actually used this script to solve a problem he faced. The Scripting Guys actually practicing what they preach? Bet you never thought you\u2019d see that, did you?<\/P>\n<P>Of course, not being the smartest of Scripting Guys he came very close to <I>not<\/I> using the script to solve his problem. As is his wont, this Scripting Guy read your email and wrote a script several days before he actually sat down to write this column. In the interim, he faced a situation very similar to yours: he had a folder with over 100 files in it, files that had names like this:<\/P><PRE class=\"codeSample\">Slide 1.gif\nSlide 2.gif\nSlide 3.gif\n<\/PRE>\n<P>The problem with that? Because these files were destined for the Web, all the blank spaces in all those file names had to be removed. As you might expect, this Scripting Guy opened the folder in question, right-clicked the first file, clicked <B>Rename<\/B>, and began manually renaming each and every file.<\/P>\n<P>It was right then that a little voice in his head said, \u201cHow dumb <I>are<\/I> you?\u201d At first he ignored the little voice; after all, he\u2019s been asked that question so many times he no longer bothers to answer. <I>(Editor\u2019s Note: And usually the voice is coming from the Scripting Editor, who he tends to ignore<\/I><I> anyway<\/I><I>.)<\/I> This time, though, the voice grew a little more insistent. \u201cWhy are you manually removing blank spaces from all these file names? Why don\u2019t you just use the script you wrote the other day?\u201d<\/P>\n<P>\u201cUm, what script is that, little voice?\u201d<\/P>\n<P>\u201c<I>The script that removes blank spaces from file names!<\/I>\u201d<\/P>\n<P>Oh. <I>This<\/I> script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Data&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    arrNames = Split(objFile.Name, &#8220;\\&#8221;)\n    intIndex = Ubound(arrNames)\n    arrNames(intIndex) = Replace(arrNames(intIndex), &#8221; &#8220;, &#8220;&#8221;)\n    strNewName = Join(arrNames, &#8220;\\&#8221;)\n    errResult = objFile.Rename(strNewName)\nNext\n<\/PRE>\n<P>With a little help from that voice inside our head let\u2019s see if we can explain how this script works. We begin by connecting to the WMI service on the local computer (although this script can also be run against a remote computer). We then use this query to return a collection consisting of all the files in the folder C:\\Data:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Data&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Now it gets interesting. To begin with, we set up a For Each loop to loop through the collection of files. Inside this loop we start out by executing this line of code:<\/P><PRE class=\"codeSample\">arrNames = Split(objFile.Name, &#8220;\\&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is using the <B>Split<\/B> method to split the file path (or, as WMI refers to it, the <B>Name<\/B> property) on the \\. For example, if the first file in the collection is C:\\Data\\File 1.pdf we\u2019ll end up with an array (arrNames) consisting of the following elements:<\/P><PRE class=\"codeSample\">C:\nData\nFile 1.pdf\n<\/PRE>\n<P>Why do we bother with that? Because we need to separate the file name (File 1.pdf) from the rest of the path, something that has to be done before we start removing blank spaces. What difference does that make? In this particular script, no difference. But suppose our file had this file path:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\Ken Myer\\My Scripts\\File 1.pdf\n<\/PRE>\n<P>If we did a blanket replacement of all the blank spaces in the path we\u2019d end up with this:<\/P><PRE class=\"codeSample\">C:\\DocumentsandSettings\\KenMyer\\MyScripts\\File1.pdf\n<\/PRE>\n<P><I>That\u2019s<\/I> why we need to extract the file name, remove the blank spaces <I>just from the file name<\/I>, and then put the complete file path back together.<\/P>\n<P>We see we have a question: how does putting the file name into an array help us with this chore? Well, we know that the file name has to be the last item in the file path; that means it will also be the last item in the array. By determining the <B>Ubound<\/B> (upper bound) value of the array, we can determine the index number for the file name:<\/P><PRE class=\"codeSample\">intIndex = Ubound(arrNames)\n<\/PRE>\n<P>Once we know the index number we can use this line of code to remove any blank spaces found in the file name:<\/P><PRE class=\"codeSample\">arrNames(intIndex) = Replace(arrNames(intIndex), &#8221; &#8220;, &#8220;&#8221;)\n<\/PRE>\n<P>It\u2019s a complicated-looking line of code, but it\u2019s actually doing something quite simple. What we\u2019re doing here is assigning a new value to the last item in the array, <B>arrNames(intIndex)<\/B>. What\u2019s the new value being assigned? This:<\/P><PRE class=\"codeSample\">Replace(arrNames(intIndex), &#8221; &#8220;, &#8220;&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is using the <B>Replace<\/B> function to replace any blank spaces (&#8221; &#8220;) in arrNames(intIndex) with, well, nothing (&#8220;&#8221;). The net effect: any blank spaces found in the file name will be removed.<\/P>\n<P>With the blank spaces gone we can then use the <B>Join <\/B>function to reconstruct the file path:<\/P><PRE class=\"codeSample\">strNewName = Join(arrNames, &#8220;\\&#8221;)\n<\/PRE>\n<P>Notice that we pass Join an optional, second parameter: <B>\u201c\\\u201d<\/B>. Why? Well, by default, the Join method puts a blank space between each of the items being joined; that would mean we\u2019d end up with a file path like this:<\/P><PRE class=\"codeSample\">C: Data File1.pdf\n<\/PRE>\n<P>And you\u2019re right: that isn\u2019t much of a file path, is it? By using this second parameter we\u2019re telling the Join method to place a \\ between each item instead of a blank space. That gives us a file path like <I>this<\/I>:<\/P><PRE class=\"codeSample\">C:\\Data\\File1.pdf\n<\/PRE>\n<P>Which is more like it.<\/P>\n<P>As soon as we have a new file path (with the blank spaces removed from the file name) we can then use this line of code to rename the file C:\\Data\\File 1.pdf to C:\\data\\File1.pdf:<\/P><PRE class=\"codeSample\">errResult = objFile.Rename(strNewName)\n<\/PRE>\n<P>And then we loop around and repeat the process with the next file in the collection.<\/P>\n<P>There you go, JRP; we hope this script proves as useful to you as it did to us. And speaking of us, now the little voice inside our head is telling us to go get a couple doughnuts and then sneak out early. And while we probably shouldn\u2019t do it, well, the voice <I>was<\/I> right about using this script, wasn\u2019t it?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a folder with a bunch of files in it, files with names like Doc 1.pdf, Doc 2.pdf, and Doc 3.pdf. How do I remove the blank spaces from all those file names?&#8212; JRP Hey, JRP. You know, today\u2019s column represents a landmark in Hey, Scripting Guy! history. Why\u2019s that? Because [&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":[715,38,3,12,5],"class_list":["post-66673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-associators-of","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a folder with a bunch of files in it, files with names like Doc 1.pdf, Doc 2.pdf, and Doc 3.pdf. How do I remove the blank spaces from all those file names?&#8212; JRP Hey, JRP. You know, today\u2019s column represents a landmark in Hey, Scripting Guy! history. Why\u2019s that? Because [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66673","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=66673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66673\/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=66673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}