{"id":67603,"date":"2006-04-05T11:17:00","date_gmt":"2006-04-05T11:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/05\/how-can-i-determine-the-path-to-the-folder-where-a-script-is-running\/"},"modified":"2006-04-05T11:17:00","modified_gmt":"2006-04-05T11:17:00","slug":"how-can-i-determine-the-path-to-the-folder-where-a-script-is-running","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-path-to-the-folder-where-a-script-is-running\/","title":{"rendered":"How Can I Determine the Path to the Folder Where a Script is Running?"},"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 the path to the folder where a script is running? What I\u2019d like to do is have the script automatically open the folder where the script is running.<BR><BR>&#8212; PD<\/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, PD. You know, for the Scripting Guys this question &#8211; as do so many of the questions we receive &#8211; falls under the category Why Didn\u2019t We Think of That? The Scripting Guys spend a lot of time testing scripts, including scripts submitted by members of the <A href=\"http:\/\/null\/technet\/scriptcenter\/csc\/default.mspx\"><B>Windows scripting community<\/B><\/A>. (Well, <I>one<\/I> of the Scripting Guys spends a lot of time testing scripts. Not that he\u2019s complaining, mind you, even though he has to do pretty much <I>all<\/I> the work around here. <I>[Editor\u2019s Note: Including most of the whining.]<\/I>) Many of these scripts add files to or delete files from a folder or write information to a log file in the same folder where the script itself can be found. That means we often need to have Windows Explorer opened to the script\u2019s home folder. How do we open that folder? Well, we double-click <B>My Computer<\/B>, we double-click <B>C:<\/B>, we double-click <B>Scripts<\/B>, etc., etc. It never occurred to us to have the script go ahead and open up the folder for us.<\/P>\n<P>You know, someone really should write a daily column explaining how to do useful little scripting tasks like that. That would be a handy thing to have, wouldn\u2019t it?<\/P>\n<P>So maybe the Scripting Guys aren\u2019t particularly good at thinking up new ideas. However, once someone else has come up with an idea we can usually help them turn that idea into a script:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>strPath = Wscript.ScriptFullName<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.GetFile(strPath)\nstrFolder = objFSO.GetParentFolderName(objFile) <\/p>\n<p>strPath = &#8220;explorer.exe \/e,&#8221; &amp; strFolder\nobjShell.Run strPath\n<\/PRE>\n<P>As you can see, this isn\u2019t a particularly complicated script. We begin by creating an instance of the <B>Wscript.Shell<\/B> object; we\u2019ll use this object later to launch Windows Explorer. We then use this line of code to get the full path to the running script (for example, C:\\Scripts\\My_script.vbs):<\/P><PRE class=\"codeSample\">strPath = Wscript.ScriptFullName\n<\/PRE>\n<P>Before we go any further, a quick clarification. Some of you might be wondering why we didn\u2019t use the <B>CurrentDirectory<\/B> property to determine the current directory of the script. Well, we could have, except for one thing: the current directory of the script isn\u2019t necessarily the same folder in which the script is running. After all, you can easily start a script in C:\\Scripts and then change the current directory to C:\\Test. For this column we wanted the name of the folder where the script resides. If you\u2019d prefer to get the current directory then use this script instead:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nstrPath = objShell.CurrentDirectory<\/p>\n<p>strPath = &#8220;explorer.exe \/e,&#8221; &amp; strPath\nobjShell.Run strPath\n<\/PRE>\n<P>Now, back to our original script. After storing the full path to the script in the variable strPath we next create an instance of the <B>Scripting.FileSystemObject<\/B>. That brings us to these two lines of code:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.GetFile(strPath)\nstrFolder = objFSO.GetParentFolderName(objFile)\n<\/PRE>\n<P>At the moment we have the path to the script file itself: C:\\Scripts\\My_script.vbs. That\u2019s fine, but what we really want to do is get rid of the file name and leave us with just the folder path: C:\\Scripts. Although there are several different ways we could do this, we felt this approach was the simplest. What we do here is use the <B>GetFile<\/B> method to bind to the file C:\\Scripts\\My_script.vbs (notice how we pass the variable strPath as the sole parameter to GetFile). After making the connection to the file we can then use the <B>GetParentFolderName<\/B> method to get the full path to the folder where the file resides; needless to say, that happens to be C:\\Scripts. And that also happens to be the very folder we want to open. <\/P>\n<P>The rest is easy. With the path to the script folder safely tucked away in a variable named strPath, we can then use this line of code to construct a command line string that will open a Windows Explorer window, a window that has the focus set to C:\\Scripts:<\/P><PRE class=\"codeSample\">strPath = &#8220;explorer.exe \/e,&#8221; &amp; strPath\n<\/PRE>\n<P>All that\u2019s left to do now is call the <B>Run<\/B> method, passing along the command line string we just constructed:<\/P><PRE class=\"codeSample\">objShell.Run strPath\n<\/PRE>\n<P>Cool, huh? Now, anyone out there have an idea for <I>tomorrow\u2019s<\/I> column?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the path to the folder where a script is running? What I\u2019d like to do is have the script automatically open the folder where the script is running.&#8212; PD Hey, PD. You know, for the Scripting Guys this question &#8211; as do so many of the questions we [&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-67603","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! How can I determine the path to the folder where a script is running? What I\u2019d like to do is have the script automatically open the folder where the script is running.&#8212; PD Hey, PD. You know, for the Scripting Guys this question &#8211; as do so many of the questions we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67603","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=67603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67603\/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=67603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}