tags:

views:

29

answers:

3

hi all

How to get the path of the local directory that I run my VB script

For example

I have text.vb script I need to add to this script VB code that finds the PATH of the directory that I run the test.vb script (like pwd for LINUX/UNIX)

THX

Yael

A: 

I think this would work:

Dim currDir
Set fso = CreateObject("Scripting.FileSystemObject")
currDir = fso.GetParentFolderName(Wscript.ScriptFullName)

Edit: Or maybe you can just do this:

Replace(WScript.ScriptFullName, WScript.ScriptName, "")
ho1
THX , how to add the currDir in the follwoing syntax?If (fso.FileExists("currDir\test.vbs")) Then ...
yael
ho1
+1  A: 

Here is how:

Function ExtractFilePath(PathName)
   For x = Len(PathName) To 1 Step -1
      If Mid(PathName, x, 1) = "\" Then Exit For
   Next
   ExtractFilePath = Left (PathName, x - 1)
End Function

ExtractFilePath(WScript.ScriptFullName) 'get current path
Sarfraz
A: 

I think what you actually want is WshShell.CurrentDirectory.

Be aware that the script path may not necessarily be the current directory. It will be if you choose to set it up that way, but it's possible to run a script from a different folder.

Given this script CurrDir.vbs in C:\scripts:

' CurrDir.vbs
' show current dir as opposed to script dir

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Say "current dir = " & shl.CurrentDirectory
Say "script name = " & WScript.ScriptFullName

sub Say(s)
  WScript.Echo s
end sub

with C:\scripts in the PATH environment variable (and ".vbs" in PATHEXT and CScript as default host), then when run from the C:\test folder, this will be the result:

C:\test>CurrDir
current dir = C:\test
script name = C:\scripts\CurrDir.vbs
Todd