• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Basename without extension

  • Thread starter Keith Hill [MVP]
  • Start date
K

Keith Hill [MVP]

#1
This is one area where I think Korn shell is easier - determining the basename of a script san path and extension.

SCRIPTNAME=$(basename $0 .ksh)

versus the PowerShell way:

$ScriptName = [system.io.path]::GetFilenameWithoutExtension($MyInvocation.InvocationName)

What I would like to see is something like this:

$ScriptName = Split-Path $MyInvocation.InvocationName -leaf -noExt

It's not dramaticall fewer characters to type however you don't have to drop down to the .NET framework to strip an extension off a filename.

--
Keith
 

My Computer

M

mikes.net

#2
Well, there's always this:

(split-path $path.ps1 -leaf).split(".")[0]

Not terribly unreasonable, but I agree that a noExtension would be
nice, and actually, expected.

Mike


On Apr 9, 11:55 am, "Keith Hill [MVP]"
<r_keith_h...@no.spam.thank.u.hotmail.com> wrote:
> This is one area where I think Korn shell is easier - determining the basename of a script san path and extension.
>
> SCRIPTNAME=$(basename $0 .ksh)
>
> versus the PowerShell way:
>
> $ScriptName = [system.io.path]::GetFilenameWithoutExtension($MyInvocation.InvocationName)
>
> What I would like to see is something like this:
>
> $ScriptName = Split-Path $MyInvocation.InvocationName -leaf -noExt
>
> It's not dramaticall fewer characters to type however you don't have to drop down to the .NET framework to strip an extension off a filename.
>
> --
> Keith
 

My Computer

J

Jacques Barathon [MS]

#3
"mikes.net" <michael.net@gmail.com> wrote in message
news:1176138028.791197.20160@w1g2000hsg.googlegroups.com...
> Well, there's always this:
>
> (split-path $path.ps1 -leaf).split(".")[0]
>
> Not terribly unreasonable, but I agree that a noExtension would be
> nice, and actually, expected.


Personally I have extended the File type so that I can do this:

PS> $file = "myscript.ps1"
PS> (gci $file).basename
myscript

Basename is a very simple ScriptProperty that says this:

[IO.Path]::GetFileNameWithoutExtension($this)

Maybe this property should be included as a default. Feel free to submit the
suggestion if it makes sense to you.

Jacques
 

My Computer