{"id":63993,"date":"2007-09-19T02:12:00","date_gmt":"2007-09-19T02:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/09\/19\/how-can-i-move-files-off-a-users-desktop-and-into-specified-folders\/"},"modified":"2007-09-19T02:12:00","modified_gmt":"2007-09-19T02:12:00","slug":"how-can-i-move-files-off-a-users-desktop-and-into-specified-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-move-files-off-a-users-desktop-and-into-specified-folders\/","title":{"rendered":"How Can I Move Files Off a User\u2019s Desktop and Into Specified Folders?"},"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! For various reasons, we don\u2019t want our users storing \u201cloose\u201d files on their desktop. How can I write a script that will take all the files on the user\u2019s desktop and, based on the file extension, move those files to the appropriate desktop folder? For example, if I have a file with a .doc file extension I\u2019d like to move it to a folder named Doc. If I have a file with a .xls file extension I\u2019d like to move it to a folder named XLS. And so on.<BR><BR>&#8212; GA<\/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, GA. You know, this is a great time to be alive, at least for those of us in the Seattle area. For example, on Saturday our very own Washington Huskies \u2013 oh, never mind: after leading Ohio State 7-3 at the half, the Huskies fell completely apart in the final two quarters and ended up losing their first game of the year. However, our very own Seattle Seahawks \u2013 well, the less said about our very own Seattle Seahawks the better. (And people thought it was <I>impossible<\/I> to lose to the Arizona Cardinals.) Fortunately, though, we still have our very own Seattle Mariners, who are right in the thick of the baseball pennant race.<\/P>\n<P>Or at least they <I>were<\/I>, until they lost 17 out of their past 22 games. <\/P>\n<TABLE class=\"dataTable\" id=\"EED\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. On the bright side, the Mariners became the first team in baseball history to be more than 20 games over .500 going into late August and then lose 13 out of 14 games (and, in the process, fall hopelessly out of the race). So at least we got to see history being made.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<TABLE class=\"dataTable\" id=\"EPD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Yet another note<\/B>. Did it help that Scripting Guy Dean Tsaltas, who\u2019s moved back to Halifax, Nova Scotia, sent us a photo this morning, a photo showing Nova Scotians frolicking at the beach over the weekend? Oh, sure, that helped a <I>lot<\/I> \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>All right, so maybe this isn\u2019t a <I>great<\/I> time to be alive. But, then again, there are far more important things in life than sports, right? Not that we can think of any right offhand, mind you \u2013 oh, wait: we have one. What about this, what about a script that can move files off the desktop and into folders that correspond to the file extensions?<\/P>\n<P>You know, a script like this one: <\/P><PRE class=\"codeSample\">Const DESKTOP = &amp;H10&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)<\/p>\n<p>Set objFolder = objShell.Namespace(DESKTOP)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set 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='&#8221; &amp; strpath &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    strDesktopFolder = strPath &amp; &#8220;\\&#8221; &amp; objFile.Extension\n    If objFSO.FolderExists(strDesktopFolder) Then\n        strTarget = strDesktopFolder &amp; &#8220;\\&#8221; \n        objFSO.MoveFile objFile.Name, strTarget\n    Else\n        Set objFolder = objFSO.CreateFolder(strDesktopFolder)\n        strTarget = strDesktopFolder &amp; &#8220;\\&#8221; \n        objFSO.MoveFile objFile.Name, strTarget\n    End If\nNext\n<\/PRE>\n<P>As you can see, we start things off by defining a constant named DESKTOP and setting the value to &amp;H10&amp;; we\u2019ll need this constant when we make our connection to the user\u2019s desktop folder. And that\u2019s a good point: we <I>should<\/I> note that this script only moves files stored on the user\u2019s desktop folder (e.g., C:\\Documents and Settings\\kenmyer\\Desktop). As you know, the Windows desktop actually displays items drawn from two locations: the individual user\u2019s desktop folder <I>and<\/I> the All Users desktop folder (C:\\Documents and Settings\\All Users\\Desktop). What if you want to move files from the All Users desktop folder as well the user\u2019s desktop folder? Well, in that case, the easiest thing to do is to make a duplicate copy of the script, then replace the first line of code with this:<\/P><PRE class=\"codeSample\">Const ALL_USERS_DESKTOP = &amp;H19&amp;\n<\/PRE>\n<P>And, of course, make sure you replace any instances of the constant DESKTOP with the constant ALL_USERS_DESKTOP.<\/P>\n<P>If you don\u2019t care about the All Users desktop, well, then you don\u2019t have to do anything at all.<\/P>\n<P>After defining the constant, we create an instance of the <B>Shell.Application<\/B> object, then use the following three lines of code to bind to the desktop folder, retrieve the path to that folder, and store the path (e.g., C:\\Documents and Settings\\kenmyer\\Desktop) in a variable named strPath:<\/P><PRE class=\"codeSample\">Set objFolder = objShell.Namespace(DESKTOP)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path\n<\/PRE>\n<P>If you\u2019re familiar with the Shell.Application object you\u2019re probably thinking, \u201cHmmm, the Shell object is designed to work with the local computer; does that mean this <I>script<\/I> is designed to work with the local computer?\u201d The answer to that is this: yes, this script <I>is<\/I> designed to work with the local computer. There are a couple reasons for this, with the big one being the fact that it\u2019s not always easy to identify who\u2019s logged on to a remote machine; in turn, that makes it <I>really<\/I> hard to determine which folder happens to be that user\u2019s desktop folder. This isn\u2019t necessarily impossible, but it\u2019s a bit tricky, at best. Therefore, we decided to limit this script to working on the local computer only.<\/P>\n<TABLE class=\"dataTable\" id=\"EAF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. So does that mean you\u2019re out of luck when it comes to managing remote computers? Of course not; just run this script as a user logon or logoff script and everything will work just fine.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we retrieve the path to the user\u2019s desktop folder, we connect to the WMI service on the local computer. We then use this query to return a collection of all the files found in the desktop folder:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; strpath &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>As soon as we have our collection in hand we create an instance of the <B>Scripting.FileSystemObject <\/B>object (the technology of choice for moving files around). Once <I>that<\/I> object has been created we\u2019re ready to set up a For Each loop to loop through the collection of files and \u2013 tah-dah! \u2013 start moving these files to their designated folders.<\/P>\n<P>See? As the saying goes, \u2018tis far better to strike a match than curse the darkness.<\/P>\n<P>Although, after this past weekend, it\u2019s probably a good thing that the Scripting Guy who writes this column doesn\u2019t actually <I>have<\/I> any matches,<\/P>\n<P>In order to move files off the desktop, the first thing we do inside the loop is execute this line of code:<\/P><PRE class=\"codeSample\">strDesktopFolder = strPath &amp; &#8220;\\&#8221; &amp; objFile.Extension\n<\/PRE>\n<P>As you might recall, we want to move all the desktop files into the appropriate folder. What <I>is<\/I> the appropriate folder? That\u2019s easy: it\u2019s the folder that has the same name as the file extension. (For example, all .doc files need to be moved into a folder named Doc.) What we\u2019re doing with the preceding line of code is taking a peek at the file\u2019s <B>Extension<\/B> property (which is simply the file extension, minus the period), then using that value to construct a file path similar to this one:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\Desktop\\Doc\n<\/PRE>\n<P>As you can see, that\u2019s nothing more than a folder named Doc, located in the user\u2019s desktop folder.<\/P>\n<P>Of course, we can\u2019t move a file into a folder unless that folder exists. (Something to do with the whole space-time continuum, which is yet another thing to Scripting Guy who writes this column is depressed about.) Therefore, our next step is to call the <B>FolderExists<\/B> method and determine whether or not we already <I>have<\/I> a Doc folder on the desktop:<\/P><PRE class=\"codeSample\">If objFSO.FolderExists(strDesktopFolder) Then\n<\/PRE>\n<P>Let\u2019s assume that we do. In that case, we\u2019re going to run these two lines of code:<\/P><PRE class=\"codeSample\">strTarget = strDesktopFolder &amp; &#8220;\\&#8221; \nobjFSO.MoveFile objFile.Name, strTarget\n<\/PRE>\n<P>In the first line, we\u2019re simply tacking a \\ onto the end of our Doc folder; that\u2019s going to result in the variable strTarget being assigned this value:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\Desktop\\Doc\\\n<\/PRE>\n<P>Why do we do that? Well, this ensures that our file gets copied <I>into<\/I> the Doc folder. If we leave the \\ off, we\u2019ll get a \u201cFile already exists error.\u201d Why? Because, without the trailing slash, the file system object will try to rename our .doc file to the name of the folder (C:\\Documents and Settings\\kenmyer\\Desktop\\Doc). Granted, that\u2019s a little weird (and even more irritating), but that\u2019s also the way it works. But, hey, there\u2019s no point in getting upset over a little thing like that; just put a \\ at the end of the folder path and everything will work just fine.<\/P>\n<TABLE class=\"dataTable\" id=\"EZG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Save your disgruntlement for something important. Like football games.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In line 2, we call the <B>MoveFile<\/B> method and actually move the file off the desktop and into the designated folder. And then we simply loop around and repeat the process with the next file in the collection.<\/P>\n<P>That\u2019s nice, but what if the folder Doc <I>doesn\u2019t<\/I> exist? Once again, no problem; in that case we run <I>this<\/I> block of code instead:<\/P><PRE class=\"codeSample\">Set objFolder = objFSO.CreateFolder(strDesktopFolder)\nstrTarget = strDesktopFolder &amp; &#8220;\\&#8221; \nobjFSO.MoveFile objFile.Name, strTarget\n<\/PRE>\n<P>As you can see, we really only do one thing different here: in the first line we call the <B>CreateFolder<\/B> method to create the new desktop folder. (Remember? C:\\Documents and Settings\\kenmyer\\Desktop\\Doc?) As soon as the folder is created we can assign the folder path (plus the trailing \\) the variable strTarget, then call the MoveFile method to move the file into the folder.<\/P>\n<P>And then it\u2019s back to the top of the loop where we repeat the process with the next file in the collection.<\/P>\n<P>We hope that answers your question, GA. And we hope that things are going better for you than they are for the Scripting Guys. But, then again, we have to admit that, other than a few lost football games and baseball games, things are going pretty good for the Scripting Guys. Take the weather, for example. Yesterday it \u2013 well, OK, yesterday it rained. And it\u2019s raining today. And it\u2019s supposed to rain again tomorrow. But that\u2019s all right: even in Seattle it can\u2019t rain <I>forever<\/I>, can it? <\/P>\n<P>Really? We were afraid you were going to say that. <\/P>\n<P>If anyone needs us, we\u2019ll be in the corner of our office. <\/P>\n<P>Feeling sorry for ourselves.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! For various reasons, we don\u2019t want our users storing \u201cloose\u201d files on their desktop. How can I write a script that will take all the files on the user\u2019s desktop and, based on the file extension, move those files to the appropriate desktop folder? For example, if I have a file with [&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":[16,47,3,5],"class_list":["post-63993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! For various reasons, we don\u2019t want our users storing \u201cloose\u201d files on their desktop. How can I write a script that will take all the files on the user\u2019s desktop and, based on the file extension, move those files to the appropriate desktop folder? For example, if I have a file with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63993","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=63993"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63993\/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=63993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}