{"id":71593,"date":"2004-08-23T11:48:00","date_gmt":"2004-08-23T11:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/08\/23\/can-i-change-the-current-directory-when-running-a-script\/"},"modified":"2004-08-23T11:48:00","modified_gmt":"2004-08-23T11:48:00","slug":"can-i-change-the-current-directory-when-running-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/can-i-change-the-current-directory-when-running-a-script\/","title":{"rendered":"Can I Change the Current Directory When Running a Script?"},"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! Can I change the current directory while my script is running?<\/p>\n<p>&#8212; KO, Kalispell, MT<\/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, KO. Yes, you can change the current directory while a script is running, provided you are running Windows Script Host 5.6, that is. To change the current directory, all you have to do is create an instance of the Wscript.Shell object, and then set the value of the CurrentDirectory property to the desired directory. For example, this little two-line script changes the current directory to C:\\Temp:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"WScript.Shell\")\nobjShell.CurrentDirectory = \"C:\\Temp\"\n<\/pre>\n<p>To verify that this works, copy this script, save it to some folder (doesn\u2019t matter which one, as long as it isn\u2019t C:\\Temp) and then run it. You should see two items get echoed to the screen: the name of the folder where you started the script from, and then the new current directory, C:\\Temp:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"WScript.Shell\")\nWScript.Echo objShell.CurrentDirectory\nobjShell.CurrentDirectory = \"C:\\Temp\"\nWScript.Echo objShell.CurrentDirectory\n<\/pre>\n<p>If the folder C:\\Temp doesn\u2019t exist, you\u2019ll get a file not found error.<\/p>\n<p>Incidentally, as implied above the current directory for a script isn\u2019t necessarily the folder where the script lives, it\u2019s the folder you were in when you started the script. Suppose the script is in C:\\Scripts. You open a command prompt window, change to the Windows directory, and then type this:<\/p>\n<pre class=\"codeSample\">cscript c:\\scripts\\my_script.vbs\n<\/pre>\n<p>In that case, the current directory will be C:\\Windows, <i>not<\/i> C:\\Scripts.<\/p>\n<p>For more information about the CurrentDirectory property, see the <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/script56\/html\/wslrfCurrentDirectory.asp\" target=\"_blank\">Windows Script Host documentation<\/a> on MSDN.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Can I change the current directory while my script is running? &#8212; KO, Kalispell, MT Hey, KO. Yes, you can change the current directory while a script is running, provided you are running Windows Script Host 5.6, that is. To change the current directory, all you have to do is create an [&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-71593","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! Can I change the current directory while my script is running? &#8212; KO, Kalispell, MT Hey, KO. Yes, you can change the current directory while a script is running, provided you are running Windows Script Host 5.6, that is. To change the current directory, all you have to do is create an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71593","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=71593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71593\/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=71593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}