{"id":66823,"date":"2006-07-26T15:13:00","date_gmt":"2006-07-26T15:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/26\/how-can-i-move-the-oldest-file-in-a-folder-to-a-different-folder\/"},"modified":"2006-07-26T15:13:00","modified_gmt":"2006-07-26T15:13:00","slug":"how-can-i-move-the-oldest-file-in-a-folder-to-a-different-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-move-the-oldest-file-in-a-folder-to-a-different-folder\/","title":{"rendered":"How Can I Move the Oldest File in a Folder to a Different Folder?"},"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! How can I move the oldest file in the folder C:\\Upload to C:\\Download?<BR><BR>&#8212; DD<\/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, DD. Funny you should mention the word \u201coldest.\u201d The Scripting Guy who writes this column recently attended a family reunion and, for the first time ever, came face-to-face with the whole idea of getting old. This Scripting Guy has fond memories of family reunions past, recalling the good times he had with his Scripting Siblings and Scripting Cousins. Of course, there was always one problem with family get-togethers: you had to try and steer clear of all the old people, the grownups who wanted to rub your head, tell you that they could remember back when you were in diapers, and insist on telling you a long, confusing story about your dad, a story that didn\u2019t make a bit of sense. (It would only be sometime later when you\u2019d find out they had gotten you mixed up with someone else, and the story was really about your grandpa or uncle or some obscure cousin you never heard of.) But as long as you stayed away from the grownups section, well, family reunions were fun.<\/P>\n<P>At this latest family reunion, however, this Scripting Guy noticed something very disturbing: his siblings and cousins had turned into the old people! Now <I>they<\/I> were the ones who, instead of playing Wiffle ball or badminton, sat around and talked, swapping recipes and complaining about their knees. They shied away from the cake (too rich for my blood!) and the fried chicken, and spent most of their time drinking coffee, despite the fact that the temperature was right around 100 degrees. Somewhere along the line, all the Scripting Siblings and Cousins had gotten old!<\/P>\n<P>Fortunately, that didn\u2019t happen to the Scripting Guy. He\u2019s as young and as vibrant as ever.<\/P>\n<P>And because he <I>is<\/I> still as young as ever, this Scripting Guy has no qualms about writing a script that gets rid of something old; after all, what does being old have to do with <I>him<\/I>? For example, here\u2019s a script that moves the oldest file in the folder C:\\Upload to the folder C:\\Download:<\/P><PRE class=\"codeSample\">Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.GetFolder(&#8220;C:\\Upload&#8221;)<\/p>\n<p>Set colFiles = objFolder.Files<\/p>\n<p>dtmOldestDate = Now<\/p>\n<p>For Each objFile in colFiles\n    If objFile.DateCreated &lt; dtmOldestDate Then\n        dtmOldestDate = objFile.DateCreated\n        strOldestFile = objFile.Path\n    End If\nNext<\/p>\n<p>objFSO.MoveFile strOldestFile, &#8220;C:\\Download\\&#8221;\n<\/PRE>\n<P>Before we explain how this script works we should note that DD specifically talked about moving a file from one folder on the local computer (C:\\Upload) to another folder on the same computer (C:\\Download). Because of that we went ahead and used the FileSystemObject to tackle this chore. By using the FileSystemObject we were able to quickly piece together a script that not only works but is also fairly short and easy to understand. The only drawback to using the FileSystemObject? That pretty much limits the script to running on the local computer.<\/P>\n<P>But what if you need to run this script against a remote computer? Well, at the end of this column we\u2019ll show you a variation of this script that uses WMI; that enables the script to be used against remote machines. Of course, because of a few WMI eccentricities, that also makes the script a tad bit longer and a shade more complicated. We won\u2019t take the time today to explain how the WMI script works; if you need that kind of clarification let us know and we\u2019ll revisit this sometime in the near future.<\/P>\n<P>No thank you, no potato salad for us. We\u2019ll just have coffee.<\/P>\n<P>As for the script that we <I>will<\/I> try to explain, our first thought upon reading this question was this: we\u2019ll just write a script that grabs all the files in the folder, sorts them by file creation time, and then grabs the oldest file from that sorted list. Admittedly, that\u2019s probably the way you <I>should<\/I> do it. Unfortunately, though, VBScript doesn\u2019t have any sorting capabilities built into it; that means we\u2019d have to write code that used something like a bubble sort or a disconnect recordset in order to get a sorted list of files. (For more information about these sorting techniques, see the Scripting Week 2 webcast <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032268755&amp;culture=en-US\" target=\"_blank\"><B>Things the Scripting Guys Never Told You<\/B><\/A>.) <\/P>\n<P>Either of those techniques will work, of course. However, some of that coding can be a bit complicated, and to what end? After all, we don\u2019t really <I>need<\/I> a sorted list of files; we just need to determine the oldest file in the folder. Therefore, we decided to take a less-elegant \u2013 but much easier \u2013 approach: instead of sorting all the files we\u2019d simply look at the creation time for each file, one-by-one. We\u2019d start with file 1 and keep a note of the file path; after all, seeing as how it\u2019s the only file we\u2019d looked at so far it would, by default, be the oldest file. We\u2019d then look at the creation time for file 2; if that time was older than file 1\u2019s creation time then we\u2019d keep a note regarding the path to file 2 instead. This process would continue until we\u2019d looked at all the files and determined the oldest one in the bunch. <\/P>\n<P>Which will probably be the cranky old file telling the younger files to go play in the front yard so the grownups can talk.<\/P>\n<P>To do all that we begin by creating an instance of <B>Scripting.FileSystemObject<\/B>; we then use this line of code to bind to the folder C:\\Upload:<\/P><PRE class=\"codeSample\">Set objFolder = objFSO.GetFolder(&#8220;C:\\Upload&#8221;)\n<\/PRE>\n<P>After we\u2019ve made a connection to the Upload folder we can then use this line of code to retrieve a collection of all the files found in the folder:<\/P><PRE class=\"codeSample\">Set colFiles = objFolder.Files\n<\/PRE>\n<P>Now the fun begins. What we need to do is pick a baseline time to start with; if the creation time for the first file in the collection is older than the baseline time then we\u2019ll call file 1 \u2013 for the time being \u2013 the oldest file in the collection. To get the starting baseline, we create a variable named dtmOldestDate and assign that variable the current date and time:<\/P><PRE class=\"codeSample\">dtmOldestDate = Now\n<\/PRE>\n<P>Once we\u2019ve established this baseline time we set up a For Each loop to loop through all the files in the collection. For the first file in the collection we use this line of code to see if the value of the file\u2019s <B>DateCreated<\/B> property is older than the baseline value dtmOldestDate:<\/P><PRE class=\"codeSample\">If objFile.DateCreated &lt; dtmOldestDate Then\n<\/PRE>\n<P>Needless to say, it\u2019s likely that this first file <I>will<\/I> have a creation time older than dtmOldestDate. But suppose it doesn\u2019t; what then? No big deal; in that case we simply loop around and check the next file in the collection.<\/P>\n<P>For the sake of argument, however, let\u2019s say that dtmOldestDate has the value <B>7\/25\/2006 8:00 AM<\/B> and the first file in the collection (a.txt) has a creation date of <B>7\/24\/2006 7:30 AM<\/B>. In this case, the file a.txt truly is older than the baseline value. Therefore, we execute these two lines of code:<\/P><PRE class=\"codeSample\">dtmOldestDate = objFile.DateCreated\nstrOldestFile = objFile.Path\n<\/PRE>\n<P>We\u2019re doing two things here. First, we\u2019re assigning dtmOldestDate a new value, the value of a.txt\u2019s DateCreated property. Why? Because this value now represents the oldest file in the folder. Second, we assign the path of the file (C:\\Upload\\a.txt) to a variable named strOldestFile. We use the variable dtmOldestDate to keep track of the oldest creation time; likewise, we use the variable strOldestFile to keep track of <I>which<\/I> file (by file path) is the oldest file in the folder.<\/P>\n<P>And then, of course, we simply loop around and repeat this process with the next file in the collection. When all is said and done strOldestFile will contain the path to the oldest file in the folder. In turn, that means we can move the file using one line of code:<\/P><PRE class=\"codeSample\">objFSO.MoveFile strOldestFile, &#8220;C:\\Download\\&#8221;\n<\/PRE>\n<P>Nothing too complicated there: we simply call the <B>MoveFile<\/B> method, using strOldestFile to represent the file we want to move, and using <B>C:\\Download\\<\/B> (note the \\ at the end) to indicate the Folder we want to move the file to. There you have it: the oldest file in C:\\Upload will now reside in C:\\Download.<\/P>\n<P>Now, where were we? Oh, that\u2019s right: we were going to tell you a story about your mother, weren\u2019t we? Well, you won\u2019t believe this, but your mother was a bit of a rapscallion when she was younger; she was always getting into mischief. For example, one time she wrote a WMI script that could identify and move the oldest file in a folder. That\u2019s right: your mother did that. The script looked something like this:<\/P><PRE class=\"codeSample\">Const CONVERT_TO_LOCAL_TIME = True<\/p>\n<p>Set dtmOldestDate = CreateObject(&#8220;WbemScripting.SWbemDateTime&#8221;)\ndtmOldestDate.SetVarDate Now, CONVERT_TO_LOCAL_TIME<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set FileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Upload&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In FileList<\/p>\n<p>    If objFile.CreationDate &lt; dtmOldestDate Then\n        dtmOldestDate = objFile.CreationDate\n        strOldestFile = objFile.Name\n    End If\nNext<\/p>\n<p>strFile = Replace(strOldestFile, &#8220;\\&#8221;, &#8220;\\\\&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * from CIM_DataFile where Name = &#8216;&#8221; &amp; strFile &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    strOldName = LCase(objFile.Name)\n    strNewName = Replace(strOldName, &#8220;c:\\upload&#8221;, &#8220;c:\\download&#8221;)\n    objFile.Copy(strNewName)\n    objFile.Delete\nNext\n<\/PRE>\n<P>Of course, because this script uses the <B>SWbemDateTime<\/B> object it runs on only Windows XP and Windows Server 2003. But that was just like your mother: all she ever talked about was Windows XP this and Windows Server 2003 that. For example, there was this one time \u2013 this was shortly after the war, mind you \u2013 when she came to our house \u2013 that was back when we lived on 38<SUP>th<\/SUP>, in the blue house, you remember, your dad used to bring you by every now and then and you\u2019d pick blackberries in the backyard \u2013 boy, you liked those blackberries although you didn\u2019t like the thorns, there was this one time \u2013 this is such a cute story \u2013 there was this one time \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I move the oldest file in the folder C:\\Upload to C:\\Download?&#8212; DD Hey, DD. Funny you should mention the word \u201coldest.\u201d The Scripting Guy who writes this column recently attended a family reunion and, for the first time ever, came face-to-face with the whole idea of getting old. This Scripting [&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,40,11,3,12,5],"class_list":["post-66823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-filesystemobject","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I move the oldest file in the folder C:\\Upload to C:\\Download?&#8212; DD Hey, DD. Funny you should mention the word \u201coldest.\u201d The Scripting Guy who writes this column recently attended a family reunion and, for the first time ever, came face-to-face with the whole idea of getting old. This Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66823","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=66823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66823\/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=66823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}