Truncate or Trim characters if file extension is longer than x amoun

Welcome Forums General PowerShell Q&A Truncate or Trim characters if file extension is longer than x amoun

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
3 years, 4 months ago.

  • Author
    Posts
  • #28347

    Participant
    Points: 0
    Rank: Member

    I'm trying to figure out a way to truncate/trim characters from a file extension if the file extension is greater than x amount, but I'm not sure how to. As of now, I can identify the files with the long extensions, now I need to add logic to trim to certain amount.

    so basically if any files are found with extensions greater than 5, truncate/trim to 5 characters...

    below is what I have, so far, to find files that are greater than x amount...


    Function NuixTruncateLongFileExtension
    {
    param
    (
    [Parameter(Position = 1, Mandatory=$true)] [string]$filePath,
    [Parameter(Position = 2, Mandatory=$true)] [int]$extLength
    )

    $fileName = Get-ChildItem "$filePath" -Recurse -File | Where-Object {$_.Extension.Length -gt $extLength}

    ForEach ($Name in $fileName)
    {

    Write-Host $Name

    }

  • #28349

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    After running this:

    $fileName contains one or more file objects. That means the variable name is misleading; I would change it to $files.

    That means enumerating them is correct:

    ForEach ($Name in $fileName)
    {
    
    Write-Host $Name
    
    }
    

    However, again, the variable names are misleading. I would do this:

    foreach ($file in $files) {
    }
    

    Again, assuming you use $files as recommended above.

    Within that, I would then probably use Rename-Item.

    $file | Rename-Item -New "$($file.basename).$($file.extension.substring(0,3))"
    

    That would trim to 3 characters in the extension. Happy to explain any of that – I want to make sure you understand WHY and WHAT that's all doing, not just copy and paste it ;).

  • #28350

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    After running this:

    $fileName contains one or more file objects. That means the variable name is misleading; I would change it to $files.

    That means enumerating them is correct:

    ForEach ($Name in $fileName)
    {
    
    Write-Host $Name
    
    }
    

    However, again, the variable names are misleading. I would do this:

    foreach ($file in $files) {
    }
    

    Again, assuming you use $files as recommended above.

    Within that, I would then probably use Rename-Item.

    $file | Rename-Item -New "$($file.basename).$($file.extension.substring(0,3))"

    That would trim to 3 characters in the extension. Happy to explain any of that – I want to make sure you understand WHY and WHAT that's all doing, not just copy and paste it ;).

  • #28351

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    After running this:

    $fileName = Get-ChildItem "$filePath" -Recurse -File | Where-Object {$_.Extension.Length -gt $extLength}
    

    $fileName contains one or more file objects. That means the variable name is misleading; I would change it to $files.

    That means enumerating them is correct:

    ForEach ($Name in $fileName)
    {
    
    Write-Host $Name
    
    }
    

    However, again, the variable names are misleading. I would do this:

    foreach ($file in $files) {
    }
    

    Again, assuming you use $files as recommended above.

    Within that, I would then probably use Rename-Item.

    $file | Rename-Item -New "$($file.basename).$($file.extension.substring(0,3))"
    

    That would trim to 3 characters in the extension. Happy to explain any of that – I want to make sure you understand WHY and WHAT that's all doing, not just copy and paste it ;).

  • #28363

    Participant
    Points: 306
    Helping Hand
    Rank: Contributor

    Can you explain why you are doing this? Don is showing you how to rename a file truncated, but this seems like a fix for a bigger issue that you are having.

  • #28370

    Participant
    Points: 0
    Rank: Member

    This used to be a comment, but I deleted it due to a mistake I made:

    I thought that PowerShell counts the total number of characters after the first period as the file extension, so files having more than one period like MSFT_xServiceResource.strings.psd1 would return a larger number, but this is not the case. The engine correctly lists the extension length based on the substring after the last period.

  • #28372

    Participant
    Points: 0
    Rank: Member

    Well, this is a solution we're trying to come up with because we have to ingest data into an application that has a field limitation issue, where if the extension is longer than x amount, the application will stall out and eventually crash. This is something we don't control. Also,the users have only complained about the long character extensions, not the later, where their could be more than one (.). My question about what Don sent, just so I'm clear is:


    "$($file.basename).$($file.extension.substring(0,3))"

    So this is basically, from the extensions, going to substring it to 3 characters from the (.) correct? So if the limitation for the field is 12, I can just put (0,12)???

  • #28382

    Participant
    Points: 0
    Rank: Member

    To test this, I put to files in a folder:

    Install Adobe Reader 6.exe_jfkeofnm5748fkgddofjdr
    ReadMe.htm_kfjthgnthrifkdjfogjfeee

    and then I ran my function and the result was:

    I changed to 2 and then ran it against some PDF's and the result renamed each extensions as:

    ..pd

    ReadMe..ht

    the other file didn't get touched.

    the function looks like:

    Function NuixTruncateLongFileExtension
    {
    param
    (
    [Parameter(Position = 1, Mandatory=$true)] [string]$filePath,
    [Parameter(Position = 2, Mandatory=$true)] [int]$extLength
    )

    $files = Get-ChildItem "$filePath" -Recurse -File | Where-Object {$_.Extension.Length -gt $extLength}

    ForEach ($file in $files)
    {

    Write-Host $file
    $file | Rename-Item -New "$($file.basename).$($file.extension.substring(0,3))"

    }

    }

    NuixTruncateLongFileExtension -filePath "C:\TEstData\PDFs" -extLength 27

    what is causing this extra period to be added

  • #28383

    Participant
    Points: 0
    Rank: Member

    I changed to:

    $file | Rename-Item -New "$($file.basename).$($file.extension.substring(1,3))"

    and that seemed to do it...

  • #28384

    Participant
    Points: 0
    Rank: Member

    The period itself counts as part of the Extension property (it's character zero in the property Extension, which is of type String)

    > $a = Get-ChildItem
    > $a.FullName
    C:\Users\sysop\Desktop\test\MSFT_xServiceResource.strings.psd1
    > $a.Extension
    .psd1
    >$a.Extension.Substring(0,1)
    .
    

The topic ‘Truncate or Trim characters if file extension is longer than x amoun’ is closed to new replies.