{"id":68093,"date":"2006-01-26T14:50:00","date_gmt":"2006-01-26T14:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/26\/how-can-i-auto-delete-specific-file-types-from-the-my-pictures-folder\/"},"modified":"2006-01-26T14:50:00","modified_gmt":"2006-01-26T14:50:00","slug":"how-can-i-auto-delete-specific-file-types-from-the-my-pictures-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-auto-delete-specific-file-types-from-the-my-pictures-folder\/","title":{"rendered":"How Can I Auto-Delete Specific File Types from the My Pictures 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 auto-delete specific file types from the My Pictures folder? If an error occurs with my fax machine, .drg and .txt files get added to My Pictures. I then have to delete these files before the fax machine will work again.<BR><BR>&#8212; WW<\/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, WW. You know, this was a case where the Scripting Guys almost outsmarted themselves. (True, the average hamster would have a pretty good chance of outsmarting the Scripting Guys. But that\u2019s not the point.) When we first sat down to try and answer this question we wrote a complicated script that relied on WMI event monitoring, a script that required a WQL query overflowing with single and double quotes, and which called for file paths similar to this:<\/P><PRE class=\"codeSample\">C:\\\\\\\\Documents and Settings\\\\\\\\kenmyer\\\\\\\\My Documents\\\\\\\\My Pictures\n<\/PRE>\n<P>Eep! It all worked, but we had no idea how we were going to explain such a complicated script in this little column.<\/P>\n<P>And then it dawned on us: this is a simple task (periodically remove selected files from a single folder). That means we ought to be able to solve the problem using a simple script. (OK, technically the hamster in the office next door came up with this idea. But we still had to write the script; after all, if nothing else we can type a little faster than he can.) Here\u2019s a much simpler way to carry out this chore:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const MY_PICTURES = &amp;H27&amp;\nConst DeleteReadOnly = TRUE<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(MY_PICTURES)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path<\/p>\n<p>Do Until i = 1\n    strFiles = strPath &amp; &#8220;\\*.txt&#8221;\n    objFSO.DeleteFile(strFiles), DeleteReadOnly<\/p>\n<p>    strFiles = strPath &amp; &#8220;\\*.drg&#8221;\n    objFSO.DeleteFile(strFiles), DeleteReadOnly<\/p>\n<p>    Wscript.Sleep 60000\nLoop\n<\/PRE>\n<P>Granted, what we\u2019re doing here isn\u2019t really auto-deleting files, at least not if \u201cauto-deleting\u201d means deleting a file the split-second it gets created. In this case, we have a script that connects to the My Pictures folder and periodically deletes any .txt and .drg files it finds there. We\u2019ve configured the script to do its check-and-delete thing every 60 seconds (that is, every 60,000 milliseconds), but you can adjust that value as you see fit.<\/P>\n<P>As for the code itself, we start things off the <B>On Error Resume Next<\/B> statement. This statement is very important. In order to keep the script simple, we don\u2019t bother verifying whether or not there actually <I>are<\/I> any .txt or .drg files in the My Pictures folder; we just have code that deletes those files. If it turns out that no such files exist, an error will be generated: without the On Error Resume Next statement that error would cause the script to blow up. With this statement, however, the script simply shrugs its shoulders and keeps going.<\/P>\n<P>Next we define a pair of constants:<\/P><PRE class=\"codeSample\">Const MY_PICTURES = &amp;H27&amp;\nConst DeleteReadOnly = TRUE\n<\/PRE>\n<P>MY_PICTURES is assigned the value &amp;H27&amp;; this is the value used by the Shell object to locate the My Pictures folder. Granted, we could simply hardcode in a path like this:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\My Documents\\My Pictures\n<\/PRE>\n<P>By using the Shell object and the MY_PICTURES constant, however, we can create a \u201cgeneric\u201d script that will work on any computer, regardless of the actual path to the My Pictures folder.<\/P>\n<P>Meanwhile, the constant DeleteReadOnly tells the FileSystemObject to go ahead and delete any read-only files found in the My Pictures folder. This is also very important: if we leave this out then the script will crash if it encounters any read-only .txt or .drg files. And we\u2019re guessing you don\u2019t really want your script to crash.<\/P>\n<P>Next we create instances of the <B>Scripting.FileSystemObject<\/B> and the <B>Shell.Application<\/B> objects. We use the Shell.Application object to determine the path to the My Pictures folder and store that path in a variable named strPath; that\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objFolder = objShell.Namespace(MY_PICTURES)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path\n<\/PRE>\n<P>As you can see, we call the <B>Namespace<\/B> method, passing this method the constant MY_PICTURES (which tells the Shell object which special folder to bind to). In order to get the path to the folder, we call the <B>Self<\/B> method; this creates an object reference to the actual folder. Once we have that object reference (objFolderItem) we can then store the value of the <B>Path<\/B> property in the variable strPath.<\/P>\n<P>Got all that? Good. Finally we have the following Do loop, which loops until i = 1:<\/P><PRE class=\"codeSample\">Do Until i = 1\n    strFiles = strPath &amp; &#8220;\\*.txt&#8221;\n    objFSO.DeleteFile(strFiles), DeleteReadOnly<\/p>\n<p>    strFiles = strPath &amp; &#8220;\\*.drg&#8221;\n    objFSO.DeleteFile(strFiles), DeleteReadOnly<\/p>\n<p>    Wscript.Sleep 60000\nLoop\n<\/PRE>\n<TABLE id=\"EBE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. When <I>will<\/I> i equal 1? Never; there isn\u2019t any code that changes the value of i (and, because i was never assigned a value, it takes on the default value of 0). This allows us to create an endless loop that will run forever. The only way to break out of the loop is to terminate the script process.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Time for a quick review. As you might recall, the variable strPath contains a value similar to this:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\My Documents\\My Pictures\n<\/PRE>\n<P>In order to delete all the .txt files in the folder (our first task) we need to construct a path that looks like this, tacking <B>\\*.txt <\/B>to the end of the path:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\My Documents\\My Pictures\\*.txt\n<\/PRE>\n<P>And that\u2019s what we do with this line of code, storing the new value in a variable named strFiles:<\/P><PRE class=\"codeSample\">strFiles = strPath &amp; &#8220;\\*.txt&#8221;\n<\/PRE>\n<P>We can then use the FileSystemObject\u2019s <B>DeleteFile<\/B> method to delete all the .txt files in the folder:<\/P><PRE class=\"codeSample\">objFSO.DeleteFile(strFiles), DeleteReadOnly\n<\/PRE>\n<P>Notice that we pass DeleteFile two parameters: the full path to the set of files and the constant DeleteReadOnly. We do the exact same thing for .drg files, then use this line of code to pause the script for 60 seconds:<\/P><PRE class=\"codeSample\">Wscript.Sleep 60000\n<\/PRE>\n<P>When 60 seconds are up we loop around and do the same thing all over again, once more deleting any .txt and .drg files found in the My Pictures folder.<\/P>\n<P>And by the way, thanks: we <I>do<\/I> believe that, when it comes to scripting, at least, the Scripting Guys are just as good as the average hamster. Maybe not better, but just as good.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I auto-delete specific file types from the My Pictures folder? If an error occurs with my fax machine, .drg and .txt files get added to My Pictures. I then have to delete these files before the fax machine will work again.&#8212; WW Hey, WW. You know, this was a case [&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":[122,123,3,707,94,5],"class_list":["post-68093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphics","tag-multimedia","tag-scripting-guy","tag-shell-application","tag-special-folders","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I auto-delete specific file types from the My Pictures folder? If an error occurs with my fax machine, .drg and .txt files get added to My Pictures. I then have to delete these files before the fax machine will work again.&#8212; WW Hey, WW. You know, this was a case [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68093","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=68093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68093\/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=68093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}