Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - How to convert %~dp0

Reply
 
Old 09-07-2006   #1 (permalink)
=?Utf-8?B?R2lsIE5vdmFr?=
Guest


 
 

How to convert %~dp0


For those who may be unfamiliar, if I run a dos batch script that's not in
my current working directory, then I can include this in the script to refer
to the directory that the script is actually in:
%~dp0

Is there a way to do this in PowerShell?

Thanks,
Gil

My System SpecsSystem Spec
Old 09-07-2006   #2 (permalink)
Mark [exmsft]
Guest


 
 

Re: How to convert %~dp0

Hello Gil,

> For those who may be unfamiliar, if I run a dos batch script that's
> not in
> my current working directory, then I can include this in the script to
> refer
> to the directory that the script is actually in:
> %~dp0
> Is there a way to do this in PowerShell?
>
> Thanks,
> Gil


from a previous post, we know that you can do this:

$0 = $myInvocation.MyCommand.Definition

from there, we can get:

$dp0 = [System.IO.Path]::GetDirectoryName($0)

of course, you can name the var anything you want....

thanks,
mark


My System SpecsSystem Spec
Old 09-07-2006   #3 (permalink)
=?Utf-8?B?R2lsIE5vdmFr?=
Guest


 
 

Re: How to convert %~dp0


Thank you!

Is this part of some documentation anywhere? i.e. Is there someplace I
could find information like this aside from this forum?


My System SpecsSystem Spec
Old 09-07-2006   #4 (permalink)
=?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?=
Guest


 
 

Re: How to convert %~dp0

www.microsoft.com/PowerShell

Look also for documentation Set there

Greetings /\/\o\/\/


"Gil Novak" wrote:

>
> Thank you!
>
> Is this part of some documentation anywhere? i.e. Is there someplace I
> could find information like this aside from this forum?
>
>

My System SpecsSystem Spec
Old 09-07-2006   #5 (permalink)
Alex K. Angelopoulos [MVP]
Guest


 
 

Re: How to convert %~dp0


"Mark [exmsft]" <MarkIngalls@nospam.nospam> wrote in message
news:c3aa33fbe50dc8c8a099ce9c4846@msnews.microsoft.com...
> Hello Gil,
>


> from a previous post, we know that you can do this:
>
> $0 = $myInvocation.MyCommand.Definition
>
> from there, we can get:
>
> $dp0 = [System.IO.Path]::GetDirectoryName($0)



Piggybacking on this, if you want simpler access to this information, there
are a couple of things you can do.

(1) Check out the Microsoft Connect site; Jeffrey Snover himself has noted
that supporting similar accelerators is an important feature to add if at
all possible, and I believe that the %~/d/p/n[#] accelerators have already
been pointed out. (I do the same thing myself to make WSH/Perl scripts run
with correct stdin redirection, by using "shadow" cmd scripts containing the
line 'cscript "%~dpn0.wsf" %*' or 'perl "%~dpn0.pl" %*').

(2) It is also possible to directly update the InvocationInfo type with a
scripted property to make this more easily accessible. For example, you can
add code to generate the script directory to your file usertypes.ps1xml, and
automatically load it from your profile by adding the line
Update-TypeData usertypes.ps1xml

The content for the file could look like this:

<?xml version="1.0" encoding="utf-8" ?>
<Types>

<Type>
<Name>System.Management.Automation.InvocationInfo</Name>
<Members>
<ScriptProperty>
<Name>ScriptDirectory</Name>
<GetScriptBlock>
[System.IO.Path]::GetDirectoryName($this.MyCommand.Definition)
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>

</Types>

And you could then get the script directory as the property
$MyInvocation.ScriptDirectory

Not as compact as the cmd shell accelerator at this point, of course.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
convert xls to pdf PowerShell
Convert .Swf to .Exe General Discussion
RE: Convert VHD to ISO? Virtual PC
how does one convert Vista General
Convert EML to DBX? Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46