![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||