Windows Vista Forums

Basename without extension

  1. #1


    Keith Hill [MVP] Guest

    Basename without extension

    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 System SpecsSystem Spec

  2. #2


    mikes.net Guest

    Re: Basename without extension

    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 System SpecsSystem Spec

  3. #3


    Jacques Barathon [MS] Guest

    Re: Basename without extension

    "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 System SpecsSystem Spec

  4. #4


    Keith Hill Guest

    Re: Basename without extension

    "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.


    I submitted this. Vote on it if you like:

    https://connect.microsoft.com/feedba...9716&SiteID=99

    --
    Keith


      My System SpecsSystem Spec

  5. #5


    Sung M Kim Guest

    Re: Basename without extension

    seems like a great time saver "IMO"


      My System SpecsSystem Spec


Basename without extension
Similar Threads
Thread Forum
.sig extension Browsers & Mail
pps extension Vista General
wmv Extension Vista mail
Suggestion: Add a Basename property to System.IO.FileInfo PowerShell
New extension Vista mail