{"id":70213,"date":"2005-03-17T17:24:00","date_gmt":"2005-03-17T17:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/17\/how-can-i-move-files-based-on-their-file-extension\/"},"modified":"2005-03-17T17:24:00","modified_gmt":"2005-03-17T17:24:00","slug":"how-can-i-move-files-based-on-their-file-extension","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-move-files-based-on-their-file-extension\/","title":{"rendered":"How Can I Move Files Based on Their File Extension?"},"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! I have a folder with a bunch of files in it. I need to move all those files; the only problem is that the files need to be moved to different folders depending on their file extensions. For example, I want all the .log files to go here, all the .bak files to go there, etc. How can I do that using a script?<BR><BR>&#8212; SH<\/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, SH. There are at least three ways to do this that we know of: you can use WMI; you can use the FileSystemObject; or you can use the Shell object. Is any one method better than the other? Well, ultimately all three will accomplish the task; the one advantage WMI has over the other two is that WMI can accomplish this task just as easily on remote computers as it can on the local computer. Of course, the one <I>disadvantage<\/I> WMI has is that it doesn\u2019t actually have a built-in method for moving files. But, hey, we\u2019ve never let a little thing like that stop us before, have we?<\/P>\n<P>Let\u2019s take a look at a script that retrieves a list of all the files in the C:\\Payroll folder, and then moves any file with a .log file extension to the folder D:\\Operation Logs:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Payroll&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    If objFile.Extension = &#8220;log&#8221; Then\n        strCopy = &#8220;D:\\Operation Logs\\&#8221; &amp; objFile.FileName _\n            &amp; &#8220;.&#8221; &amp; objFile.Extension\n        objFile.Copy(strCopy)\n        objFile.Delete\n    End If\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E5C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Important<\/B>. If you run this script, make sure you use valid paths. Suppose you try copying files to D:\\Operation Logs, only you don\u2019t actually <I>have<\/I> a D drive on your machine. In that case, the Copy command will fail, but the Delete command will succeed. Thus your file will be deleted without being copied anywhere.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We begin by connecting to the WMI service, then use an ASSOCIATORS OF query to retrieve all the files found in the folder C:\\Payroll. We then walk through the collection of files, checking to see if any of the files have the file extension <B>log<\/B>. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">If objFile.Extension = &#8220;log&#8221; Then\n<\/PRE>\n<P>Notice that we\u2019re not looking for files with a <B>.log<\/B> file extension; make sure you leave the dot (.) out.<\/P>\n<P>And what happens if we find a file that has a <B>log<\/B> file extension? Well, as we mentioned earlier, WMI doesn\u2019t have a built-in method for moving files. We\u2019re going to work around this by copying the file from the C:\\Payroll folder to the D:\\Operation Logs folder. For a brief moment we\u2019ll have two copies of the file: one in C:\\Payroll and one in D:\\Operation Logs. But don\u2019t worry: in the very next line of code we\u2019ll delete the copy found in C:\\Payroll. When we do that we\u2019ll have only a single copy of the file, the one found in D:\\Operation Logs. In effect, we\u2019ve \u201cmoved\u201d the file even though we never used a Move method of any kind. <\/P>\n<P>Yes, <I>very<\/I> sneaky.<\/P>\n<P>The only tricky thing here is that WMI requires a complete path name when copying a file. Suppose we find a file named March.log. To copy this file we need to use the complete path: D:\\Operation Logs\\March.log. So we use this line of code to construct that path:<\/P><PRE class=\"codeSample\">strCopy = &#8220;D:\\Operation Logs\\&#8221; &amp; objFile.FileName _\n    &amp; &#8220;.&#8221; &amp; objFile.Extension\n<\/PRE>\n<P>All we\u2019re doing here is assigning a value to the variable strCopy. That value happens to consist of <B>D:\\Operation Logs\\<\/B> plus the name of the file (<B>March<\/B>) plus a period (<B>.<\/B>) plus the file extension (<B>log<\/B>). Put them all together and it spells <B>D:\\Operation Logs\\March.log<\/B>. And that\u2019s exactly the path we need to pass to the Copy method.<\/P>\n<P>And, yes, as you probably noticed, the file extension is a separate property from the file name. That\u2019s why we have to combine the <B>FileName<\/B> property <I>plus<\/I> a period<I> plus<\/I> the <B>Extension<\/B> property. It\u2019s a bit of a hassle in this script, but it does make it easier to write other scripts, like a script that returns a list of all the files on a computer that have a <B>log<\/B> file extension. <\/P>\n<P>As soon as we\u2019ve constructed the path we can copy the file to drive D and then delete the original file from drive C. That\u2019s what we do in these two lines of code:<\/P><PRE class=\"codeSample\">objFile.Copy(strCopy)\nobjFile.Delete\n<\/PRE>\n<P>And that\u2019s it: the script loops around until it has checked each file in the folder, and until it has moved all the files with a <B>log<\/B> file extension to D:\\Operation Logs.<\/P>\n<P>In order to keep the sample script as short as possible, we check for only a single file extension. However, it\u2019s easy to check for additional file extensions and to move those files to the appropriate folders. Here\u2019s a revised script that also checks for .bak files and copies them to D:\\Operation Backups:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Payroll&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    If objFile.Extension = &#8220;log&#8221; Then\n        strCopy = &#8220;D:\\Operation Logs\\&#8221; &amp; objFile.FileName _\n            &amp; &#8220;.&#8221; &amp; objFile.Extension\n        objFile.Copy(strCopy)\n        objFile.Delete\n    End If\n    If objFile.Extension = &#8220;bak&#8221; Then\n        strCopy = &#8220;D:\\Operation Backups\\&#8221; &amp; objFile.FileName _\n            &amp; &#8220;.&#8221; &amp; objFile.Extension\n        objFile.Copy(strCopy)\n        objFile.Delete\n    End If\nNext\n<\/PRE>\n<P>If you want to check for even more file extensions, then just add some additional If-Then statements. That\u2019s all there is to it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a folder with a bunch of files in it. I need to move all those files; the only problem is that the files need to be moved to different folders depending on their file extensions. For example, I want all the .log files to go here, all the .bak files [&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-70213","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! I have a folder with a bunch of files in it. I need to move all those files; the only problem is that the files need to be moved to different folders depending on their file extensions. For example, I want all the .log files to go here, all the .bak files [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70213","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=70213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70213\/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=70213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}