VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Odd error trying to get a directory. (https://forums.vandyke.com/showthread.php?t=14501)

dsp00k 04-15-2021 12:21 PM

Odd error trying to get a directory.
 
Hi.

I am having a strange issue. When I use the following extremely basic code snippet, the return sometimes works properly, and other times I get a completely different outcome.

... platformType is passed to the function

Code:

Dim p, ppfile, ppfso, ppcmd, ppShell, ppPath
             
                ' Create the full path to the command file
                Set ppShell = CreateObject("Wscript.Shell")

                ppPath = ppShell.CurrentDirectory
                ppfile = ppPath & "\" & platformType & ".pp"

                ' Message box for Debugging Only
                msgbox ppfile

                'Open the command file
                Set ppfso = CreateObject("Scripting.FileSystemObject")
                Set p = ppfso.OpenTextFile(ppfile, ForReading)

...

It is supposed to return the path of the running script so I can call a file that is located in the same directory as the script. i.e.
c:\users\donnie\scripts and then construct the full filename which is supposed to be something like "c:\users\donnie\scripts\9k.pp"


Sometimes it works properly, and most other times I get "c:\windows\system32\9k.pp" as the returned path instead, which of course leads to a File not Found.


Thanks,
Donnie

cboyack 04-15-2021 02:48 PM

Quote:

Originally Posted by dsp00k (Post 54911)
It is supposed to return the path of the running script so I can call a file that is located in the same directory as the script. i.e.
c:\users\donnie\scripts and then construct the full filename which is supposed to be something like "c:\users\donnie\scripts\9k.pp"

Sometimes it works properly, and most other times I get "c:\windows\system32\9k.pp" as the returned path instead, which of course leads to a File not Found.

This issue is most likely being caused by the fact that Current Directory can mean quite a number of different locations, depending on a multitudinous number of factors. As a result, you'll most likely need to use a different call if you want to consistently obtain the path of the script-containing folder.

Please see this forum post for options that may alleviate this problem.

For fun, you may want to try running the script code listed there as well, just to see how easily the current directory can change depending on context.

dsp00k 04-20-2021 02:39 PM

crt.ScriptFullName
 
I see this returns the full filename but how would I chop the script name off of the end in order to end up with just the full path?

Thanks

cboyack 04-20-2021 03:35 PM

First off, I'll copy the script from the other post just so anyone else viewing can see what we're discussing:

Code:

# $language = "Python"
# $interface = "1.0"

import os
strDir1 = os.getcwd()
strDir2 = os.path.dirname(os.path.realpath("."))
strDir3 = os.path.dirname(os.path.realpath(__file__))
strDir4 = os.path.dirname(crt.ScriptFullName)
crt.Dialog.MessageBox("dir1: {}\ndir2: {}\ndir3: {}\ndir4: {}".format(
    strDir1,
    strDir2,
    strDir3,
    strDir4))

I'm a little confused as to how you're getting the full script filename within the path using any of the options listed within the above script, as each one is clearly meant to grab a directory name, rather than a file name.

I would have expected that something like:
Code:

                ppPath = os.path.dirname(crt.ScriptFullName)
...would have worked to give you the path to the containing folder.

What changes have you made to the script?

dsp00k 04-21-2021 11:13 PM

That would be in Python.

I am using VBScript...

I found the answer.

Code:

Set ppShell = CreateObject("Scripting.FileSystemObject")
ppPath = ppShell.GetParentFolderName(crt.ScriptFullName)
ppFile = ppPath & "\" & platformType & ".pp"

Thanks for your time!

Donnie

cboyack 04-22-2021 07:18 AM

Hi Donnie,

I apologize for not catching that from your initial message. Should've been clear since you were using Wscript...

Anyways, I'm glad that you were able to discover a solution to obtain the containing folder's path in VBScript.


All times are GMT -6. The time now is 06:12 AM.