Getting Current Directory

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
User avatar
drukamp
Posts: 42
Joined: Fri Oct 23, 2009 7:05 am

Getting Current Directory

Post by drukamp »

OS Version: Windows XP SP2
PrimalForms Version: 2009 1.1.2.0
PrimalScript Version: 2007 4.5.580

The issue I am having is calling a .cmd file in my Form from the same directory. I want to create a button in my Form that calls a .cmd file that resides in the same directoy that my Form runs from. I've used this code before in PrimalScript scripts and it works with no issue; but it will not run in my Form (even if I export it to a .ps1 file and run that from a .cmd file)
Command:
$Global:G_sCurrentDir = Split-Path $MyInvocation.MyCommand.Path (then to call the file I use:
Invoke-Expression -Command ($G_oCurrectDir"ScriptName.cmd")
Here is the error that I get:
Split-Path : Cannot bind argument to parameter 'Path' because it is null.


If I type in the file path (C:...ScriptName.cmd); the Invoke command works.
Your help is much appreciated!

Mike

User avatar
Alexander Riedel
Posts: 7407
Joined: Tue May 29, 2007 4:43 pm

Getting Current Directory

Post by Alexander Riedel »

Please refer to this blog article and see if that solves your problem:
http://www.sapien.com/blog/2009/09/02/p ... nvocation/
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
drukamp
Posts: 42
Joined: Fri Oct 23, 2009 7:05 am

Getting Current Directory

Post by drukamp »

I tried this and I am getting the same error message.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Current Directory

Post by davidc »

I was able to reproduce your issue, but it does function correctly when the script is packaged.

We will look into this further.

Thank you,

David
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Current Directory

Post by davidc »

This issue will be resolved in the next service release.

David
David
SAPIEN Technologies, Inc.

User avatar
drukamp
Posts: 42
Joined: Fri Oct 23, 2009 7:05 am

Getting Current Directory

Post by drukamp »

Thank you David!

User avatar
drukamp
Posts: 42
Joined: Fri Oct 23, 2009 7:05 am

Getting Current Directory

Post by drukamp »

Hi David: I installed the new update and I still cannot run this. How do I call the current directory that the script is running from?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Current Directory

Post by davidc »

To resolve this issue, use the following function in your script:
function get-scriptdirectory{
if($hostinvocation -ne $null) {
Split-Path $hostinvocation.MyCommand.path
}
else {
$invocation=(get-variable MyInvocation -Scope 1).Value
Split-Path $invocation.myinvocation.path
}
}
For a detailed explanation, please refer to the blog post Alex mentioned above.

David
David
SAPIEN Technologies, Inc.

User avatar
drukamp
Posts: 42
Joined: Fri Oct 23, 2009 7:05 am

Getting Current Directory

Post by drukamp »

The 'else' still brings a $null value if it runs in PrimalScript. I had someone here take a look and he modified the 'else' statement and now it can be used in PrimalForms, PrimalScript, and if you kick the .ps1 fill off via a .cmd file:
function get-scriptdirectory{ if($hostinvocation -ne $null) { Split-Path $hostinvocation.MyCommand.path } else { (Get-Location -PSProvider FileSystem).ProviderPath }}

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Getting Current Directory

Post by davidc »

What version of PrimalScript are you currently running? The function returns the correct path when I test it with PrimalScript 2009.

David
David
SAPIEN Technologies, Inc.