{"id":71043,"date":"2004-11-09T19:12:00","date_gmt":"2004-11-09T19:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/09\/can-i-use-a-script-to-rename-all-the-files-in-a-folder\/"},"modified":"2004-11-09T19:12:00","modified_gmt":"2004-11-09T19:12:00","slug":"can-i-use-a-script-to-rename-all-the-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/can-i-use-a-script-to-rename-all-the-files-in-a-folder\/","title":{"rendered":"Can I Use a Script to Rename All the Files in a Folder?"},"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! Can I use a script to rename all the files in a directory and include a &#8220;pl-&#8221; prefix plus the original file name?<BR><BR>&#8212; JP<\/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, JP. It\u2019s interesting how many people need to do something similar: they need to rename all the files in a given folder, either by appending the date, changing the file extension, or &#8211; in this case &#8211; tacking <B>pl-<\/B> to the beginning of each file name. Well, never let it be said that the Script Guys don\u2019t listen to Microsoft\u2019s customers. (We don\u2019t, we just don\u2019t want it to ever be said!) Here\u2019s a script that does exactly what you want:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet 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:\\Logs&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    strNewName = objFile.Drive &amp; objFile.Path &amp; &#8220;pl-&#8221; &amp; _\n        objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n    errResult = objFile.Rename(strNewName)\nNext\n<\/PRE>\n<P>If you\u2019re planning on modifying this script to meet your own needs, there are two key points to keep in mind. First, notice that we use an Associators Of query in order to return a collection of all the files found in a folder (in this case, the folder C:\\Logs):<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Logs&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>An Associators Of query does pretty much what the name implies: it enables you to associate two WMI classes. In this case, we\u2019re associating Win32_Directory (the class that lets us manage folders) with CIM_DataFile (the class that lets us manage files). You might think that the Win32_Directory class would have a property named Files that would list all the files found in that folder. For some reason, though, it doesn\u2019t. Instead, we have to use an Associators Of query to achieve the same effect.<\/P>\n<P>As you might expect, a complete discussion of Associators Of would never fit in this little column. For more information, check out this portion of the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/wmisdk\/wmi\/swbemservices_associatorsof.asp\" target=\"_blank\"><B>WMI SDK<\/B><\/A>. For now, just copy the code as-is, and &#8211; if necessary &#8211; change <B>C:\\Logs<\/B> to the appropriate folder.<\/P>\n<P>The other thing to keep in mind is the fact that when you rename a file using WMI, you must pass the entire file path to the Rename method. Suppose you want to rename the file C:\\Logs\\File_1.txt to C:\\Logs\\Pl-File_1.txt. This line of code won\u2019t do it:<\/P><PRE class=\"codeSample\">errResult = objFile.Rename(&#8220;Pl-File_1.txt&#8221;)\n<\/PRE>\n<P>Instead, you\u2019ll have to use <I>this<\/I> line of code:<\/P><PRE class=\"codeSample\">errResult = objFile.Rename(&#8220;C:\\Logs\\Pl-File_1.txt&#8221;)\n<\/PRE>\n<P>That\u2019s why the code preceding the Rename method looks so complicated: we have to construct the entire path for our new file name, assign it to the variable strNewName, then pass strNewName to the Rename method. Here\u2019s how the code breaks down:<\/P>\n<TABLE class=\"dataTable\" id=\"EUD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Drive<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Returns the drive letter (in this case C:\\) for the file we are renaming.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Path<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">Don\u2019t let the name fool you: the Path property returns only the folder paths, excluding the drive and the file name. For example, if you are connected to the file C:\\Logs\\File1_txt, the Path property returns Logs\\. If you were connected to C:\\Scripts\\Logs\\Admin Logs\\File_1.txt, the Path would be Scripts\\Logs\\Admin Logs\\.<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>&#8220;pl-&#8220;<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The prefix we want to tack on to the front of each file name.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.FileName<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The current name of the file <I>excluding<\/I> the file extension(in this case, <B>File_1<\/B>).<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>&#8220;.&#8221;<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The period that comes between the file name and the file extension. The Extension property (see below) only returns the <I>characters<\/I> included in the file extension (e.g,, <B>txt<\/B>); it does not return the period.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Extension<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">The file extension of the file. Suppose we wanted to change all these files from .txt to .log. In that case, we wouldn\u2019t need to use the existing file extension; instead, we\u2019d append <B>.log<\/B> to the end of the file name.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In other words:<\/P>\n<TABLE class=\"dataTable\" id=\"EYF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Drive<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">C:\\<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Path<\/B>Logs\\<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>&#8220;pl-&#8220;<\/B><\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">pl-<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">&nbsp;<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.FileName<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">File_1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>&#8220;.&#8221;<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">.<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>objFile.Extension<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">txt<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Add them all together &#8211; <B>C:\\ <\/B>+<B> Logs\\<\/B> + <B>pl-<\/B> + <B>File_1<\/B> + <B>.<\/B> + <B>txt<\/B> &#8211; and you have the new name for the file.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Can I use a script to rename all the files in a directory and include a &#8220;pl-&#8221; prefix plus the original file name?&#8212; JP Hey, JP. It\u2019s interesting how many people need to do something similar: they need to rename all the files in a given folder, either by appending the date, [&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,3,12,5],"class_list":["post-71043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Can I use a script to rename all the files in a directory and include a &#8220;pl-&#8221; prefix plus the original file name?&#8212; JP Hey, JP. It\u2019s interesting how many people need to do something similar: they need to rename all the files in a given folder, either by appending the date, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71043","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=71043"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71043\/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=71043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}