{"id":71683,"date":"2004-08-10T11:57:00","date_gmt":"2004-08-10T11:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/08\/10\/how-can-i-call-the-dir-command\/"},"modified":"2004-08-10T11:57:00","modified_gmt":"2004-08-10T11:57:00","slug":"how-can-i-call-the-dir-command","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-call-the-dir-command\/","title":{"rendered":"How Can I Call the Dir Command?"},"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! I\u2019m trying to do something that seems pretty simple: call the dir command from a script. It doesn\u2019t seem to work, though. How come?<\/p>\n<p>&#8212; CR, Mexico City, Mexico<\/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, CR. It appears from your mail that you\u2019re trying to do a simple thing like this:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Wscript.Shell\")\nobjShell.Run(\"dir\"), 1, TRUE\n<\/pre>\n<p>However, instead of getting a list of everything in the current folder, you\u2019re getting the message <b>The system cannot find the file specified<\/b>. What gives?<\/p>\n<p>Well, what gives is this: there really <i>isn\u2019t<\/i> a file named dir on your computer. Search for dir.exe or dir.com; you won\u2019t find it. Instead, dir is actually a command built into the command shell (cmd.exe or command.exe, depending on the version of Windows you are running). That means dir is available only when you are running the command shell. To demonstrate this, open up a command window, type <b>dir<\/b>, and press ENTER. You should get a listing of all the files and folders in the current directory. Now, bring up the <b>Run<\/b> dialog box, type <b>dir<\/b>, and press ENTER. You should get an error message like this:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/nodir.jpg\" width=\"636\" height=\"126\"><\/p>\n<p>But that doesn\u2019t mean you\u2019re out of luck. As it turns out there actually <i>is<\/i> a way to call the dir command from within a script; you just have to be clever about it. Because dir is a command built into the command shell, what you do is call the command shell and pass dir as a command-line argument. Let\u2019s take a look at a script that does the trick, then we\u2019ll explain how it works:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Wscript.Shell\")\nobjShell.Run(\"%comspec% \/k dir\"), 1, TRUE\n<\/pre>\n<p>The first line of the script simply creates an instance of the WSH Shell object, and the second line uses the Run method to actually call the dir command. But note that we don\u2019t specify dir directly; instead we specify <b>%comspec% \/k dir<\/b>. This command string is broken down like this:<\/p>\n<table id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>%comspec%<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Opens up a command window. %comspec% is an environment variable that points to the current command shell. By using %comspec% you don\u2019t have to worry about whether the command shell is cmd.exe or command.exe; %comspec% will pick the correct one.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>\/k<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Ensures that the window stays open after the dir command is called. That\u2019s what the \/k is for. If we wanted to make sure that the command window automatically closes after the dir command is called then we would change the \/k (keep) to a \/c (close).<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>dir<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Runs the dir command.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to do something that seems pretty simple: call the dir command from a script. It doesn\u2019t seem to work, though. How come? &#8212; CR, Mexico City, Mexico Hey, CR. It appears from your mail that you\u2019re trying to do a simple thing like this: Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;) objShell.Run(&#8220;dir&#8221;), 1, [&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":[2,3,4,5],"class_list":["post-71683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m trying to do something that seems pretty simple: call the dir command from a script. It doesn\u2019t seem to work, though. How come? &#8212; CR, Mexico City, Mexico Hey, CR. It appears from your mail that you\u2019re trying to do a simple thing like this: Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;) objShell.Run(&#8220;dir&#8221;), 1, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71683","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=71683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71683\/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=71683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}