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
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
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($_) }
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($_) }
>
>
>
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
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($_) }
> >
> >
> >
:: 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($_) }
>> >
>> >
>> >
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
"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
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
>
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
>>
>
>
| 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 |