{"id":70533,"date":"2005-02-01T09:03:00","date_gmt":"2005-02-01T09:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/01\/how-can-i-change-the-working-folder-of-a-script\/"},"modified":"2005-02-01T09:03:00","modified_gmt":"2005-02-01T09:03:00","slug":"how-can-i-change-the-working-folder-of-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-working-folder-of-a-script\/","title":{"rendered":"How Can I Change the Working Folder of a Script?"},"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! My script needs to have the same working folder as the application that the script starts. How can I change the working folder of a script?<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. You can change the current (or working) folder of a script simply by setting the value of the Wscript Shell object\u2019s <B>CurrentDirectory<\/B> property. (<B>Note<\/B>. The CurrentDirectory property was introduced in Windows Script Host 5.6, so the scripts we\u2019ll show you today require you to be running that version of WSH.) For example, here\u2019s a demonstration script that echoes the current directory for a script, changes the current directory to C:\\Windows, and then echoes the current directory again:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nWscript.Echo objShell.CurrentDirectory<\/p>\n<p>objShell.CurrentDirectory = &#8220;C:\\Windows&#8221;\nWscript.Echo objShell.CurrentDirectory\n<\/PRE>\n<P>As you might expect, in the last line of the script <B>C:\\Windows<\/B> is echoed as the name of the current directory.<\/P>\n<P>Now that we know how to change the current folder all we have to do is write a simple little script that starts an application and then changes the current directory to match the working folder of that application. For example, this script starts Notepad (C:\\Windows\\System32\\Notepad.exe), determines the desired working folder by parsing the path to Notepad.exe, and then changes the current directory to C:\\Windows\\System32:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>strApp = &#8220;C:\\Windows\\System32\\Notepad.exe&#8221;\narrPath = Split(strApp, &#8220;\\&#8221;)<\/p>\n<p>For i = 0 to Ubound(arrPath) &#8211; 1\n    strAppPath = strAppPath &amp; arrPath(i) &amp; &#8220;\\&#8221;\nNext <\/p>\n<p>objShell.CurrentDirectory = strAppPath\nWscript.Echo objShell.CurrentDirectory<\/p>\n<p>objShell.Run(strApp)\n<\/PRE>\n<P>All we\u2019re doing is storing the application path (C:\\Windows\\System32\\Notepad.exe) in a variable named strApp. We then use the VBScript Split function to split this path into an array (using the \\ as our delimiter); that gives us an array consisting of these elements:<\/P><PRE class=\"codeSample\">C:\nWindows\nSystem32\nNotepad.exe\n<\/PRE>\n<P>Why do we want an array consisting of these elements? Well, as you can see, the first three elements &#8211; plus a couple \\\u2019s &#8211; give us the path we want: C:\\Windows\\System32. By splitting the path like this, we can determine the folder path by taking the first three items and discarding the executable file name (Notepad.exe), That\u2019s what we do with this line of code, which reconstructs the path using all the items in the array except the very last one:<\/P><PRE class=\"codeSample\">For i = 0 to Ubound(arrPath) &#8211; 1\n    strAppPath = strAppPath &amp; arrPath(i) &amp; &#8220;\\&#8221;\nNext\n<\/PRE>\n<P>In case you\u2019re wondering, arrays always start with item 0, which is why our For-Next loop starts at 0. In turn, the <B>Ubound<\/B> property represents the last item in the array. We don\u2019t want to use the last item, we only want to go as far as the next-to-last item. Hence <B>Ubound &#8211; 1<\/B>, which simply means the last item in the array minus 1, or the next-to-last item. In the sample script shown here, that gives us this equation:<\/P>\n<P><B>C:<\/B> + <B>\\<\/B> + <B>Windows<\/B> + <B>\\<\/B> + <B>System32<\/B> + <B>\\<\/B><\/P>\n<P>Or, <B>C:\\Windows\\System32\\<\/B>. (The extra \\ on the end doesn\u2019t make any difference, so we don\u2019t bother trimming it off.)<\/P>\n<P>From there we change the current directory to C:\\Windows\\System32; echo the value of the current directory (just so you know that the change has been made); and then go ahead and start Notepad. Problem solved!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! My script needs to have the same working folder as the application that the script starts. How can I change the working folder of a script?&#8212; JM Hey, JM. You can change the current (or working) folder of a script simply by setting the value of the Wscript Shell object\u2019s CurrentDirectory property. [&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-70533","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! My script needs to have the same working folder as the application that the script starts. How can I change the working folder of a script?&#8212; JM Hey, JM. You can change the current (or working) folder of a script simply by setting the value of the Wscript Shell object\u2019s CurrentDirectory property. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70533","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=70533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70533\/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=70533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}