{"id":70343,"date":"2005-02-28T08:45:00","date_gmt":"2005-02-28T08:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/28\/how-can-i-determine-if-a-users-folder-exists-on-a-computer\/"},"modified":"2005-02-28T08:45:00","modified_gmt":"2005-02-28T08:45:00","slug":"how-can-i-determine-if-a-users-folder-exists-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-a-users-folder-exists-on-a-computer\/","title":{"rendered":"How Can I Determine if a Users&#8217; Folder Exists on a Computer?"},"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 determine if the C:\\Documents and Settings\\<I>%username%<\/I>\\Application Data\\Microsoft\\Templates folder exists on a computer?<BR><BR>&#8212; JM<\/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, JM. The FileSystemObject includes a <B>FolderExists<\/B> method that makes it very easy to check for the existence of a folder. Just pass FolderExists the complete path to the folder and you\u2019ll quickly get back True if the Folder exists and False if the folder does not exist.<\/P>\n<P>The only catch here is that you don\u2019t necessarily know the complete path to the folder; after all, each user who logs on is going to have a different path to the Templates folder; that\u2019s because <I>%username%<\/I> is different for each user. <\/P>\n<P>But that\u2019s OK: we can easily figure out the value of <I>%username%<\/I>, and then use that value to construct the path to the Templates folder. Let\u2019s show you a script that checks for the existence of the C:\\Documents and Settings\\<I>%username%<\/I>\\Application Data\\Microsoft\\Templates folder, and then we\u2019ll explain how it works:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrUser = objNetwork.UserName<\/p>\n<p>strPath = &#8220;C:\\Documents and Settings\\&#8221; &amp; strUser &amp; &#8220;\\Application Data\\Microsoft\\Templates&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>If objFSO.FolderExists(strPath) Then\n    Wscript.Echo &#8220;The folder exists.&#8221;\nElse\n    Wscript.Echo &#8220;The folder does not exist.&#8221;\nEnd If\n<\/PRE>\n<P>We begin by creating an instance of the Wscript.Network object, and then storing the value of the <B>UserName<\/B> property in a variable named strUser. Because UserName happens to have the same value as the <I>%username%<\/I> environment variable we can now construct the exact path to the Templates folder. That\u2019s what we do in this line of code, which simply adds <B>C:\\Documents and Settings\\<\/B> plus the value of strUser plus <B>\\Application Data\\Microsoft\\Templates<\/B>:<\/P><PRE class=\"codeSample\">strPath = &#8220;C:\\Documents and Settings\\&#8221; &amp; strUser &amp; &#8220;\\Application Data\\Microsoft\\Templates&#8221;\n<\/PRE>\n<P>If our user happens to be named kmyer, we\u2019ll now have a path that looks like this:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\kmyer\\Application Data\\Microsoft\\Templates\n<\/PRE>\n<P>The rest is easy. We create an instance of the FileSystemObject and use the FolderExists method to determine whether the folder C:\\Documents and Settings\\kmyer\\Application Data\\Microsoft\\Templates exists or not. We echo back the answer &#8211; yes, the folder exists or no, the folder does not exist &#8211; and we\u2019re done.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine if the C:\\Documents and Settings\\%username%\\Application Data\\Microsoft\\Templates folder exists on a computer?&#8212; JM Hey, JM. The FileSystemObject includes a FolderExists method that makes it very easy to check for the existence of a folder. Just pass FolderExists the complete path to the folder and you\u2019ll quickly get back True [&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":[11,3,12,5],"class_list":["post-70343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine if the C:\\Documents and Settings\\%username%\\Application Data\\Microsoft\\Templates folder exists on a computer?&#8212; JM Hey, JM. The FileSystemObject includes a FolderExists method that makes it very easy to check for the existence of a folder. Just pass FolderExists the complete path to the folder and you\u2019ll quickly get back True [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70343","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=70343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70343\/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=70343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}