none
Filtering the file name RRS feed

  • Question

  • Hi Guys 

    I am writing a script to extract the file name without the extension by using the below.

    $input is using the get-file to pick up the file in directory.

    $FN = split-Path -path $input -Leaf -Resolve

    $input = $FN.split('.')[0] 

    But i neglected that there are quite a number of file that use more than one period(.)

    How do i set the filter to delimit the last period before extgension without knowing how many period is in that file?


    Friday, July 29, 2016 8:23 AM

Answers

  • Hi,

    Use the BaseName property instead of trying to parse:

    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016> ls
    
    
        Directory: C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016
    
    
    Mode                LastWriteTime     Length Name                                                                                                                        
    ----                -------------     ------ ----                                                                                                                        
    -a---         7/29/2016   8:58 AM         14 Some.Long.File.Name.txt                                                                                                     
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016> (Get-Item .\Some.Long.File.Name.txt).BaseName
    Some.Long.File.Name


    • Marked as answer by Noobpie Tuesday, August 2, 2016 1:54 AM
    Friday, July 29, 2016 12:59 PM

All replies

  • Hi,

    this should work:

    $myPoint="my.very.long.filename.with.many.dots.txt"
    
    $mypoint.substring(0,$mypoint.LastIndexOf("."))



    This post is provided AS IS with no warranties or guarantees, and confers no rights.
    ~~~
    Questo post non fornisce garanzie e non conferisce diritti



    • Edited by aperelli Friday, July 29, 2016 9:06 AM
    Friday, July 29, 2016 9:06 AM
  • "$input" is a reserved variable.  You cannot use it or assign to it.


    \_(ツ)_/

    Friday, July 29, 2016 10:40 AM
  • Hi,

    Use the BaseName property instead of trying to parse:

    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016> ls
    
    
        Directory: C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016
    
    
    Mode                LastWriteTime     Length Name                                                                                                                        
    ----                -------------     ------ ----                                                                                                                        
    -a---         7/29/2016   8:58 AM         14 Some.Long.File.Name.txt                                                                                                     
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-29-2016> (Get-Item .\Some.Long.File.Name.txt).BaseName
    Some.Long.File.Name


    • Marked as answer by Noobpie Tuesday, August 2, 2016 1:54 AM
    Friday, July 29, 2016 12:59 PM