Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-15-2021, 12:21 PM
dsp00k dsp00k is offline
Registered User
 
Join Date: Apr 2019
Posts: 5
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

Last edited by cboyack; 04-15-2021 at 02:40 PM. Reason: Please use [CODE] and [/CODE] tags to denote areas of code
Reply With Quote
  #2  
Old 04-15-2021, 02:48 PM
cboyack cboyack is offline
VanDyke Technical Support
 
Join Date: Apr 2020
Location: Albuquerque, NM
Posts: 132
Quote:
Originally Posted by dsp00k View Post
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.
__________________
Thanks,
--Cameron

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 04-20-2021, 02:39 PM
dsp00k dsp00k is offline
Registered User
 
Join Date: Apr 2019
Posts: 5
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
Reply With Quote
  #4  
Old 04-20-2021, 03:35 PM
cboyack cboyack is offline
VanDyke Technical Support
 
Join Date: Apr 2020
Location: Albuquerque, NM
Posts: 132
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?
__________________
Thanks,
--Cameron

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 04-21-2021, 11:13 PM
dsp00k dsp00k is offline
Registered User
 
Join Date: Apr 2019
Posts: 5
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

Last edited by dsp00k; 04-21-2021 at 11:56 PM.
Reply With Quote
  #6  
Old 04-22-2021, 07:18 AM
cboyack cboyack is offline
VanDyke Technical Support
 
Join Date: Apr 2020
Location: Albuquerque, NM
Posts: 132
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.
__________________
Thanks,
--Cameron

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 08:15 PM.