Discussion:
How to get the current folder?
(too old to reply)
Ploflo
2004-04-26 12:40:52 UTC
Permalink
How can you get the current folder within a vbscript-program?
I worked around this problem by creating a filesystemobject with the
filename of my program and split the file-path... but if someone was
to change the filename of my program, this wouldn't be working
anymore...
Any suggestions?
Viatcheslav V. Vassiliev
2004-04-26 12:46:08 UTC
Permalink
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory

//------------------------------------
Regards,
Vassiliev V. V.
http://www-sharp.com -
Scripting/HTA/.Net Framework IDE
Post by Ploflo
How can you get the current folder within a vbscript-program?
I worked around this problem by creating a filesystemobject with the
filename of my program and split the file-path... but if someone was
to change the filename of my program, this wouldn't be working
anymore...
Any suggestions?
Miyahn
2004-04-26 13:07:28 UTC
Permalink
Post by Ploflo
How can you get the current folder within a vbscript-program?
I worked around this problem by creating a filesystemobject with the
filename of my program and split the file-path... but if someone was
to change the filename of my program, this wouldn't be working
anymore...
Any suggestions?
If you are using the word of "the current folder" in the meaning of
"the place where a script file exists", you will have only to use
"WScript.ScriptFullName".
--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP (Office Systems - Excel)
***@nifty.ne.jp
Joe Earnest
2004-04-26 18:57:11 UTC
Permalink
Hi,
Post by Ploflo
How can you get the current folder within a vbscript-program?
I worked around this problem by creating a filesystemobject with the
filename of my program and split the file-path... but if someone was
to change the filename of my program, this wouldn't be working
anymore...
Any suggestions?
CurrentDirectory requires WSH v5.6. If you're using the script across older
versions and want the current active directory, use a zero-length
GetAbsolutePathName:

set oFso= createobject("scripting.fileSystemObject")
wscript.echo oFso.getAbsolutePathName("")

Joe Earnest
Ploflo
2004-04-27 07:31:40 UTC
Permalink
Post by Joe Earnest
CurrentDirectory requires WSH v5.6. If you're using the script across older
versions and want the current active directory, use a zero-length
set oFso= createobject("scripting.fileSystemObject")
wscript.echo oFso.getAbsolutePathName("")
Thanks, this solution works best for me!

Ploflo
Miyahn
2004-04-27 10:59:28 UTC
Permalink
Post by Ploflo
Post by Joe Earnest
CurrentDirectory requires WSH v5.6. If you're using the script across older
versions and want the current active directory, use a zero-length
set oFso= createobject("scripting.fileSystemObject")
wscript.echo oFso.getAbsolutePathName("")
Thanks, this solution works best for me!
Is that true?

Save the shortcut of the following script to sendto folder,
and try to execute via contextmenu of any other file -> sendto
-> "shortcut of script file"

Set FS = CreateObject("Scripting.FileSystemObject")
MsgBox "Current Folder : " & FS.GetAbsolutePathName("") & vbCrLf & _
"Script File Folder : " & FS.GetParentFolderName(WScript.ScriptFullName)
Set FS = Nothing
--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP (Office Systems - Excel)
***@nifty.ne.jp
Joe Earnest
2004-04-27 17:00:31 UTC
Permalink
Hi,
Post by Miyahn
Post by Ploflo
Post by Joe Earnest
CurrentDirectory requires WSH v5.6. If you're using the script across older
versions and want the current active directory, use a zero-length
set oFso= createobject("scripting.fileSystemObject")
wscript.echo oFso.getAbsolutePathName("")
Thanks, this solution works best for me!
Is that true?
Save the shortcut of the following script to sendto folder,
and try to execute via contextmenu of any other file -> sendto
-> "shortcut of script file"
Set FS = CreateObject("Scripting.FileSystemObject")
MsgBox "Current Folder : " & FS.GetAbsolutePathName("") & vbCrLf & _
"Script File Folder : " &
FS.GetParentFolderName(WScript.ScriptFullName)
Post by Miyahn
Set FS = Nothing
--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP (Office Systems - Excel)
Seems to work fine, or at least like it's supposed to.
WshShell.CurrentDirectory produces *exactly* the same result. I think that
the result you see is a fluke of Windows re-setting the active directory at
the SendTo operation, and also different active directories on different
drives. The FSO and CurrentDirectory techniques both report the current,
"actual" active directory, not the initial script home or "working"
directory.

Better in this regard to add scripts to ProgIds in HKCR than in SendTo, or
pass the pathname into the LNK as an argument.

Joe Earnest
Miyahn
2004-04-27 22:23:20 UTC
Permalink
Hi, Joe.
Post by Miyahn
Hi,
Post by Miyahn
Post by Ploflo
Post by Joe Earnest
CurrentDirectory requires WSH v5.6. If you're using the script across
older
Post by Miyahn
Post by Ploflo
Post by Joe Earnest
versions and want the current active directory, use a zero-length
set oFso= createobject("scripting.fileSystemObject")
wscript.echo oFso.getAbsolutePathName("")
Thanks, this solution works best for me!
Is that true?
Save the shortcut of the following script to sendto folder,
and try to execute via contextmenu of any other file -> sendto
-> "shortcut of script file"
Set FS = CreateObject("Scripting.FileSystemObject")
MsgBox "Current Folder : " & FS.GetAbsolutePathName("") & vbCrLf & _
"Script File Folder : " &
FS.GetParentFolderName(WScript.ScriptFullName)
Post by Miyahn
Set FS = Nothing
--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP (Office Systems - Excel)
Seems to work fine, or at least like it's supposed to.
WshShell.CurrentDirectory produces *exactly* the same result. I think that
the result you see is a fluke of Windows re-setting the active directory at
the SendTo operation, and also different active directories on different
drives. The FSO and CurrentDirectory techniques both report the current,
"actual" active directory, not the initial script home or "working"
directory.
Better in this regard to add scripts to ProgIds in HKCR than in SendTo, or
pass the pathname into the LNK as an argument.
Yes I know that.

Since the current directory is not same as the initial script home in some
situation, if OP intends to handle some data files or another script files
located in the lator, FSO.GetParentFolderName(WScript.ScriptFullName)
would be suitable.
--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP (Office Systems - Excel)
***@nifty.ne.jp
Michael Harris (MVP)
2004-04-27 23:53:05 UTC
Permalink
Post by Joe Earnest
Seems to work fine, or at least like it's supposed to.
WshShell.CurrentDirectory produces *exactly* the same result.... I
Most of the time it will, but there are circumstances where the current
directory *can* be different from the script's parent folder depending on
how the script was actually launched. It all depends on exactly what you
want.

For example...

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory
WshShell.CurrentDirectory = wscript.scriptfullname & "\.."
WScript.Echo WshShell.CurrentDirectory


If you launch the script by double-clicking it from Windows Explorer, then
both echo's show the same value.

If you launch the script via the Start Menu / Run dialog with:

wscript d:\scripts\myscript.vbs
or
cscript d:\scripts\myscript.vbs

then both echo's probably won't show the same value.

If you launch the script from a console window opened on a different folder
from the script's parent folder with:

start d:\scripts\myscript.vbs

or just (on NT/2K/XP)

d:\scripts\myscript.vbs

or via a shortcut (lnk) file with an explicit 'Start in:' path different
from the script's parent folder, then both echo's will *not* show the same
value.
--
Michael Harris
Microsoft.MVP.Scripting
Sammamish WA US
Joe Earnest
2004-04-28 15:40:21 UTC
Permalink
Hi Michael.
Post by Michael Harris (MVP)
Post by Joe Earnest
Seems to work fine, or at least like it's supposed to.
WshShell.CurrentDirectory produces *exactly* the same result.... I
Most of the time it will, but there are circumstances where the current
directory *can* be different from the script's parent folder depending on
how the script was actually launched. It all depends on exactly what you
want.
For example...
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory
WshShell.CurrentDirectory = wscript.scriptfullname & "\.."
WScript.Echo WshShell.CurrentDirectory
If you launch the script by double-clicking it from Windows Explorer, then
both echo's show the same value.
wscript d:\scripts\myscript.vbs
or
cscript d:\scripts\myscript.vbs
then both echo's probably won't show the same value.
If you launch the script from a console window opened on a different folder
start d:\scripts\myscript.vbs
or just (on NT/2K/XP)
d:\scripts\myscript.vbs
or via a shortcut (lnk) file with an explicit 'Start in:' path different
from the script's parent folder, then both echo's will *not* show the same
value.
I know that the current active folder and the script's home folder may be
different. Windows resets the current folder for any number of
circumstances, both on initiation and subsequently. I think that was
sufficiently specified to the posting OP, who seemed to want (or was content
with) the current active directory. I was simply pointing out that the
FSO.getAbsolutePathName("") (zero-length string trick for pre-v5.6) and
WshShell.currentDirectory (for v5.6) functions return the same value for the
same drive. BTW, I like the [ WshShell.currentDirectory =
wscript.scriptFullName & "\.." ] setting -- I always do it the long way,
through FSO.getParentFolderName.

Regards,
Joe Earnest

Loading...