{"id":71093,"date":"2004-11-02T14:32:00","date_gmt":"2004-11-02T14:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/02\/how-can-i-delete-the-files-in-the-temporary-internet-files-folder\/"},"modified":"2004-11-02T14:32:00","modified_gmt":"2004-11-02T14:32:00","slug":"how-can-i-delete-the-files-in-the-temporary-internet-files-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-the-files-in-the-temporary-internet-files-folder\/","title":{"rendered":"How Can I Delete the Files in the Temporary Internet Files 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! Is there a way to delete all the files in the Temporary Internet Files folder using a script?<BR><BR>&#8212; KR<\/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, AK. You know, prompted by your question one of the Scripting Guys (who shall remain nameless) thought to himself, \u201cHmmm, I wonder what\u2019s in <I>my<\/I> Temporary Internet Files folder?\u201d Upon checking, he discovered the folder contained 239 megabytes worth of junk; as near as he could tell, the entire Internet was being stored on his hard disk.<\/P>\n<P>In other words, having a script that deletes all the files in the Temporary Internet Files folder sounded like a script worth having.<\/P>\n<P>The only complicating factor is trying to determine <I>where<\/I> a user has his or her Temporary Internet Files folder. Typically, you\u2019ll find this in the user\u2019s local user profile, which will usually be something like C:\\Documents and Settings\\kenmyer\\Local Settings\\Temporary Internet Files. However, the folder doesn\u2019t <I>have<\/I> to be there; after all, Windows might not even be installed on drive C. That means that the key to writing this script lies in locating the Temporary Internet Files folder; after you\u2019ve found it, deleting all the files is a snap.<\/P>\n<P>So how do you locate this folder? Well, as it turns out, the Temporary Internet Files folder is a \u201cspecial\u201d folder, a folder which is found, by default, on all installations of Windows, and a folder that the operating system keeps an eye on; no matter what you do to this folder, Windows will still know its whereabouts. Because of that, you can use the Shell object &#8211; which has the ability to locate special folders, regardless of their physical location &#8211; to determine the path to Temporary Internet Files, and then use that path to bind to the folder and delete all the files found there.<\/P>\n<P>One caveat: the Shell object can\u2019t be created remotely. That means you either need to run this script as a logon or logoff script, or copy it to the remote computer and use the WMI Win32_Process class to kick it off on that remote machine. For more information, you might want to see this <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0901.mspx\"><B>Hey, Scripting Guy!<\/B><\/A> column, which used Win32_Process to run the md command on a remote computer.<\/P>\n<P>Let\u2019s take a look at the script:<\/P><PRE class=\"codeSample\">Const TEMPORARY_INTERNET_FILES = &amp;H20&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TEMPORARY_INTERNET_FILES)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path &amp; &#8220;\\*.*&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.DeleteFile(strPath)\n<\/PRE>\n<P>Yes, we know: after that big buildup, you thought this would be a lot more complicated, didn\u2019t you? We start off by setting the constant TEMPORARY_INTERNET_FILES to &amp;H20&amp;, the value required to connect to the Temporary Internet Files folder (more on that in a minute). We then create the Shell object, and use the Namespace method to locate the folder. Due to the somewhat quirky nature of the Shell object, we then call the Self method to actually connect to the folder; that\u2019s what this line of code does:<\/P>\n<P>Set objFolderItem = objFolder.Self<\/P>\n<P>At this point, we can now determine the actual path (e.g., C:\\Documents and Settings\\kenmyer\\Local Settings\\Temporary Internet Files) to the folder in question. If that\u2019s all we wanted to know, we could just use a line of code similar to this:<\/P><PRE class=\"codeSample\">Wscript.Echo objFolderItem.Path\n<\/PRE>\n<P>However, we want to use the FileSystemObject to delete all the files in that folder. To do that, we have to pass the FileSystemObject a path similar to this, using standard wildcard characters to represent all the files in the folder:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kenmyer\\Local Settings\\Temporary Internet Files\\*.*\n<\/PRE>\n<P>And then we build that path with this line of code:<\/P><PRE class=\"codeSample\">strPath = objFolderItem.Path &amp; &#8220;\\*.*&#8221;\n<\/PRE>\n<P>As you can see, we take the path returned by the Shell object, and then we add <B>\\*.*<\/B>. The net result is a command which will tell the FileSystemObject to delete all the files in the folder.<\/P>\n<P>So that\u2019s what we do in the final two lines of code: we create an instance of the FileSystemObject, and then use the DeleteFile method to delete all the files. Just like that, we\u2019ve cleaned out our Temporary Internet Files Folder.<\/P>\n<P>Of course, as long as you\u2019re at it, you might want to clean out the Internet Cookies folder as well. Here\u2019s a script that will do <I>that<\/I>:<\/P><PRE class=\"codeSample\">Const COOKIES = &amp;H21&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(COOKIES)\nSet objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path &amp; &#8220;\\*.*&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.DeleteFile(strPath)\n<\/PRE>\n<P>Pretty cool, huh?<\/P>\n<P>Incidentally, we had a premonition that as soon as we posted this column people would start asking us, \u201cSay, how do I connect to the My Pictures folder?\u201d or \u201cIs there any way to connect to the My Recent Documents folder?\u201d Because of that, we went ahead added 38 new scripts to the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/desktop\/special\/default.mspx\"><B>Script Center Script Repository<\/B><\/A>, scripts that will show you how to connect to each of the special folders reachable using the Shell object. Enjoy!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way to delete all the files in the Temporary Internet Files folder using a script?&#8212; KR Hey, AK. You know, prompted by your question one of the Scripting Guys (who shall remain nameless) thought to himself, \u201cHmmm, I wonder what\u2019s in my Temporary Internet Files folder?\u201d Upon checking, he [&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-71093","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! Is there a way to delete all the files in the Temporary Internet Files folder using a script?&#8212; KR Hey, AK. You know, prompted by your question one of the Scripting Guys (who shall remain nameless) thought to himself, \u201cHmmm, I wonder what\u2019s in my Temporary Internet Files folder?\u201d Upon checking, he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71093","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=71093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71093\/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=71093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}