Windows Vista Forums

Get-Childitem files names without extension
  1. #1


    =?Utf-8?B?S2Jnbw==?= Guest

    Get-Childitem files names without extension

    All,



    I'm pull my hair out trying to work out the easiest way to retrieve a list
    of file names without its extension. There seems to be no propriety on type
    System.IO.FileInfo which does this. Can any body help me pls?

    I'm sure its simple.

    regards

      My System SpecsSystem Spec

  2. #2


    Jouko Kynsijärvi Guest

    Re: Get-Childitem files names without extension

    Kbgo wrote:
    > I'm pull my hair out trying to work out the easiest way to retrieve a
    > list of file names without its extension. There seems to be no
    > propriety on type System.IO.FileInfo which does this.


    gci | % { [IO.Path]::GetFileNameWithoutExtension($_) }



      My System SpecsSystem Spec

  3. #3


    =?Utf-8?B?S2Jnbw==?= Guest

    Re: Get-Childitem files names without extension

    Jouko,

    thats fantastic, however in the interests of fully understanding your answer
    (which works perfectly). What is the significance of the %?

    and the :: ? (does this call a static method?)

    Many thanks again.

    "Jouko Kynsijärvi" wrote:

    > Kbgo wrote:
    > > I'm pull my hair out trying to work out the easiest way to retrieve a
    > > list of file names without its extension. There seems to be no
    > > propriety on type System.IO.FileInfo which does this.

    >
    > gci | % { [IO.Path]::GetFileNameWithoutExtension($_) }
    >
    >
    >


      My System SpecsSystem Spec

  4. #4


    Alex K. Angelopoulos [MVP] Guest

    Re: Get-Childitem files names without extension

    One possibility is extending the FileInfo type with a Basename property;
    Jeffrey Snover discussed this in the developer blog at one point:
    http://blogs.msdn.com/powershell/arc...24/645981.aspx


    The latest form of the BaseName ScriptProperty, with corrections by Jacques
    Barathon, is this:

    if ( ($this.extension.length -gt 0)
    -and ($this.extension.length -lt $this.Name.length) )
    {
    $this.Name.Remove($this.Name.Length - $this.Extension.Length)
    }
    else
    {
    $this.Name
    }


    You can also create a filter that will take FileInfo objects as input and
    emit base names:

    filter To-BaseName
    {
    if ( ($_.Extension.length -gt 0) -and
    ($_.Extension.length -lt $_.Name.length)
    { $_.Name.Remove($_.Name.Length - $_.Extension.Length)}
    else
    { $_.Name}

    }

    If you use


    "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    news:507BF828-044B-4769-A7FE-1AA08B8C2657@microsoft.com...
    > All,
    >
    > I'm pull my hair out trying to work out the easiest way to retrieve a list
    > of file names without its extension. There seems to be no propriety on
    > type
    > System.IO.FileInfo which does this. Can any body help me pls?
    >
    > I'm sure its simple.
    >
    > regards




      My System SpecsSystem Spec

  5. #5


    =?Utf-8?B?S2Jnbw==?= Guest

    Re: Get-Childitem files names without extension

    RTFM

    % = FOREACH-OBJECT

    Still trying to understand the :: part thou

    cheers

    "Kbgo" wrote:

    > Jouko,
    >
    > thats fantastic, however in the interests of fully understanding your answer
    > (which works perfectly). What is the significance of the %?
    >
    > and the :: ? (does this call a static method?)
    >
    > Many thanks again.
    >
    > "Jouko Kynsijärvi" wrote:
    >
    > > Kbgo wrote:
    > > > I'm pull my hair out trying to work out the easiest way to retrieve a
    > > > list of file names without its extension. There seems to be no
    > > > propriety on type System.IO.FileInfo which does this.

    > >
    > > gci | % { [IO.Path]::GetFileNameWithoutExtension($_) }
    > >
    > >
    > >


      My System SpecsSystem Spec

  6. #6


    Alex K. Angelopoulos [MVP] Guest

    Re: Get-Childitem files names without extension

    :: is used to access a static member. You can use Get-Member's Static
    parameter to examine static members:

    [IO.Path] | Get-Member -Static

    "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    news:6BD4C42F-7DBC-4494-A23D-EFC936AB1C86@microsoft.com...
    > RTFM
    >
    > % = FOREACH-OBJECT
    >
    > Still trying to understand the :: part thou
    >
    > cheers
    >
    > "Kbgo" wrote:
    >
    >> Jouko,
    >>
    >> thats fantastic, however in the interests of fully understanding your
    >> answer
    >> (which works perfectly). What is the significance of the %?
    >>
    >> and the :: ? (does this call a static method?)
    >>
    >> Many thanks again.
    >>
    >> "Jouko Kynsijärvi" wrote:
    >>
    >> > Kbgo wrote:
    >> > > I'm pull my hair out trying to work out the easiest way to retrieve a
    >> > > list of file names without its extension. There seems to be no
    >> > > propriety on type System.IO.FileInfo which does this.
    >> >
    >> > gci | % { [IO.Path]::GetFileNameWithoutExtension($_) }
    >> >
    >> >
    >> >




      My System SpecsSystem Spec

  7. #7


    Jeffrey Snover [MSFT] Guest

    Re: Get-Childitem files names without extension

    Try this trick:
    Use your favorite Search engine and search for:
    PSMDTAG: FileInfo

    PSMDTAG stands for PowerShell MetaData TAG

    --
    Jeffrey Snover [MSFT]
    Windows PowerShell/Aspen Architect
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx

    "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    news:507BF828-044B-4769-A7FE-1AA08B8C2657@microsoft.com...
    > All,
    >
    > I'm pull my hair out trying to work out the easiest way to retrieve a list
    > of file names without its extension. There seems to be no propriety on
    > type
    > System.IO.FileInfo which does this. Can any body help me pls?
    >
    > I'm sure its simple.
    >
    > regards




      My System SpecsSystem Spec

  8. #8


    Jacques Barathon [MS] Guest

    Re: Get-Childitem files names without extension

    "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    news:507BF828-044B-4769-A7FE-1AA08B8C2657@microsoft.com...
    > All,
    >
    > I'm pull my hair out trying to work out the easiest way to retrieve a list
    > of file names without its extension. There seems to be no propriety on
    > type
    > System.IO.FileInfo which does this. Can any body help me pls?
    >
    > I'm sure its simple.


    Actually, Jouko's static [IO.Path]::GetFileNameWithoutExtension() applied to
    Jeffrey's extension to the FileInfo type looks best:

    <Type>
    <Name>System.IO.FileInfo</Name>
    <Members>
    <ScriptProperty>
    <Name>Basename</Name>
    <GetScriptBlock>
    [IO.Path]::GetFileNameWithoutExtension($this)
    </GetScriptBlock>
    </ScriptProperty>
    </Members>
    </Type>

    It allows you to do things like:
    dir | ft basename,extension

    If you are now wearing a big flashing question mark, read the PowerShell's
    team blog to know what to do with the above lines :-)
    https://blogs.msdn.com/powershell/ar...24/645981.aspx

    Jacques



      My System SpecsSystem Spec

  9. #9


    Jon Miller Guest

    Re: Get-Childitem files names without extension

    If what you just did was subclass FileInfo, that's pretty cool. Strange that
    you can do that considering FileInfo is sealed though... It's a twisted
    world we live in where we have the object oriented world colliding with the
    world of scripting. ;-)

    Jon

    "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    news:usaBZbbqGHA.3820@TK2MSFTNGP05.phx.gbl...
    > "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    > news:507BF828-044B-4769-A7FE-1AA08B8C2657@microsoft.com...
    >> All,
    >>
    >> I'm pull my hair out trying to work out the easiest way to retrieve a
    >> list
    >> of file names without its extension. There seems to be no propriety on
    >> type
    >> System.IO.FileInfo which does this. Can any body help me pls?
    >>
    >> I'm sure its simple.

    >
    > Actually, Jouko's static [IO.Path]::GetFileNameWithoutExtension() applied
    > to Jeffrey's extension to the FileInfo type looks best:
    >
    > <Type>
    > <Name>System.IO.FileInfo</Name>
    > <Members>
    > <ScriptProperty>
    > <Name>Basename</Name>
    > <GetScriptBlock>
    > [IO.Path]::GetFileNameWithoutExtension($this)
    > </GetScriptBlock>
    > </ScriptProperty>
    > </Members>
    > </Type>
    >
    > It allows you to do things like:
    > dir | ft basename,extension
    >
    > If you are now wearing a big flashing question mark, read the PowerShell's
    > team blog to know what to do with the above lines :-)
    > https://blogs.msdn.com/powershell/ar...24/645981.aspx
    >
    > Jacques
    >




      My System SpecsSystem Spec

  10. #10


    Alex K. Angelopoulos [MVP] Guest

    Re: Get-Childitem files names without extension

    Shhh. It's not subclassing. We just epoxied it onto the trunk.

    "Jon Miller" <jemiller@nospam.net> wrote in message
    news:OvNvItpqGHA.3248@TK2MSFTNGP04.phx.gbl...
    > If what you just did was subclass FileInfo, that's pretty cool. Strange
    > that you can do that considering FileInfo is sealed though... It's a
    > twisted world we live in where we have the object oriented world colliding
    > with the world of scripting. ;-)
    >
    > Jon
    >
    > "Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
    > news:usaBZbbqGHA.3820@TK2MSFTNGP05.phx.gbl...
    >> "Kbgo" <Kbgo@discussions.microsoft.com> wrote in message
    >> news:507BF828-044B-4769-A7FE-1AA08B8C2657@microsoft.com...
    >>> All,
    >>>
    >>> I'm pull my hair out trying to work out the easiest way to retrieve a
    >>> list
    >>> of file names without its extension. There seems to be no propriety on
    >>> type
    >>> System.IO.FileInfo which does this. Can any body help me pls?
    >>>
    >>> I'm sure its simple.

    >>
    >> Actually, Jouko's static [IO.Path]::GetFileNameWithoutExtension() applied
    >> to Jeffrey's extension to the FileInfo type looks best:
    >>
    >> <Type>
    >> <Name>System.IO.FileInfo</Name>
    >> <Members>
    >> <ScriptProperty>
    >> <Name>Basename</Name>
    >> <GetScriptBlock>
    >> [IO.Path]::GetFileNameWithoutExtension($this)
    >> </GetScriptBlock>
    >> </ScriptProperty>
    >> </Members>
    >> </Type>
    >>
    >> It allows you to do things like:
    >> dir | ft basename,extension
    >>
    >> If you are now wearing a big flashing question mark, read the
    >> PowerShell's team blog to know what to do with the above lines :-)
    >> https://blogs.msdn.com/powershell/ar...24/645981.aspx
    >>
    >> Jacques
    >>

    >
    >




      My System SpecsSystem Spec

Get-Childitem files names without extension problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
searching for files by extension SBS user Vista General 23 04 Jun 2009
Get-Childitem filter files with multiple extensions? akcorr PowerShell 5 16 Jul 2008
unable to delete Names of files after deleted files. paul Vista mail 2 21 Apr 2008
files with msu extension geotso Vista General 4 13 Apr 2008
Word files extension .shs Michael Igoe Vista installation & setup 0 12 Mar 2007