{"id":71613,"date":"2004-08-19T16:07:00","date_gmt":"2004-08-19T16:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/08\/19\/how-can-i-determine-if-a-folder-exists-on-a-computer\/"},"modified":"2004-08-19T16:07:00","modified_gmt":"2004-08-19T16:07:00","slug":"how-can-i-determine-if-a-folder-exists-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-a-folder-exists-on-a-computer\/","title":{"rendered":"How Can I Determine if a Folder Exists on a Computer?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! Is there any way to determine whether or not a specific folder exists on a computer?<\/p>\n<p>&#8212; RP, Umatilla, OR<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, RP. There are a couple ways of doing this, depending on whether you are looking for the folder on the local computer or on a remote computer, and depending on whether or not you know the exact path to the folder. Let\u2019s start with the easiest possible situation: you\u2019re looking for the folder C:\\Scripts on the local computer.<\/p>\n<p>Why is this so easy? Because the FileSystemObject has a FolderExists method designed to tell you whether or not a folder exists. All you have to do is run the method, passing it the path C:\\Scripts. If the method returns True, then the folder was found; if it returns False, then the folder was not found.<\/p>\n<p>Here\u2019s what the code looks like:<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\nIf objFSO.FolderExists(\"C:\\Scripts\") Then\n    Wscript.Echo \"The folder exists.\"\nElse\n    Wscript.Echo \"The folder does not exist.\"\nEnd If\n<\/pre>\n<p>OK, but what if you\u2019re looking for a folder on a remote computer; after all, the FileSystemObject is designed to run locally. Well, in that case, just use the WMI class Win32_Directory and look for a folder with the Name C:\\\\Scripts. <\/p>\n<p><b>Note<\/b>. That\u2019s not a misprint. When searching for files and folders using WMI, you must double any \\\u2019s found in your query. If you were looking for C:\\Scripts\\MyScripts\\AdminScripts your query would look like this:<\/p>\n<pre class=\"codeSample\">\"Select * From Win32_Directory Where Name = \" &amp; _\n    \"'C:\\\\Scripts\\\\MyScripts\\\\AdminScripts'\"\n<\/pre>\n<p>This sample script checks for the existence of the folder C:\\Scripts on the remote computer atl-ws-01. How do we know if the folder was found? Well, the script reports back the Count (the number of items found). If Count = 0, then C:\\Scripts doesn\u2019t exist; if Count = 1, then C:\\Scripts <i>does<\/i> exist.<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-ws-01\"\nSet objWMIService = GetObject _\n    (\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where \" &amp; _\n        \"Name = 'C:\\\\Scripts'\")\nWscript.Echo colFolders.Count\n<\/pre>\n<p>Ok, now for the hard one: what if you\u2019re looking for a folder named Scripts, but you have no idea whether it\u2019s on drive C or drive D or somewhere else; in other words, you can\u2019t search a specific path like C:\\Scripts. That\u2019s all right; you can use WMI to search for a folder with the FileName Scripts (the FileName property is equivalent to what we would call the folder name). Because the entire file system has to be searched this script might take a minute or so to complete (depending on how many folders you have on your machine), but it will do the trick:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-ws-01\"\nSet objWMIService = GetObject _\n    (\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where \" &amp; _\n        \"FileName = 'Scripts'\")\nWscript.Echo colFolders.Count\n<\/pre>\n<p>This script, by the way, can be used both locally and remotely. Note, too that it\u2019s possible for this script to return more than one item; that\u2019s because all these folders have the FileName <i>Scripts<\/i>, even though their paths differ:<\/p>\n<pre class=\"codeSample\">C:\\Documents and Settings\\All Users\\Documents\\Corporate\\Scripts\nC:\\Scripts\nD:\\Administrative Tools\\Scripts\nE:\\WMI\\Scripts\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to determine whether or not a specific folder exists on a computer? &#8212; RP, Umatilla, OR Hey, RP. There are a couple ways of doing this, depending on whether you are looking for the folder on the local computer or on a remote computer, and depending on whether [&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-71613","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! Is there any way to determine whether or not a specific folder exists on a computer? &#8212; RP, Umatilla, OR Hey, RP. There are a couple ways of doing this, depending on whether you are looking for the folder on the local computer or on a remote computer, and depending on whether [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71613","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=71613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71613\/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=71613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}