How to bulk delete the last n characters of all the filenames in a folder

103 Views Asked by At

I am in directory G:\TXT and I want to change the 2 file names (a01mm.txt and a02mm.txt into a01.txt and a02.txt) in folder TXT, but it doesn't work. There was no result whatsoever. Can anybody please tell me what I did wrong. I tried

gci | rni -newname { $_.name -TrimEnd(2) }
4

There are 4 best solutions below

0
Jeevan ebi On

PowerShell:

Get-ChildItem G:\TXT | Where-Object { $_.Name -match "a\d\dmm\.txt" } | ForEach-Object {
    $newName = $_.Name -replace "mm", ""
    Rename-Item -Path $_.FullName -NewName $newName
}

Or

Get-ChildItem | Rename-Item -NewName { $_.Name -replace "mm", "" }

After running this PowerShell script, the files "a01mm.txt" and "a02mm.txt" should be renamed to "a01.txt" and "a02.txt" in the "TXT" directory. Make sure you're in the correct directory when running the script, or provide the full path to the files if needed

0
The Freelancer On

@Michael van Zuuren, You can try this for your requirement

Set-Location -Path "G:\TXT"

# Get a list of files with names matching "a*mm.txt"
$filesToRename = Get-ChildItem -Filter "a*mm.txt"

# Loop through the files and rename them
foreach ($file in $filesToRename) {
    $newName = $file.Name -replace 'mm', ''
    Rename-Item -Path $file.FullName -NewName $newName
}
0
lit On

This will remove the last N characters from all filenames. When you are confident that it work correctly, remove the -WhatIf from the Rename-Item command.

NB: It will not work on filenames containing a BaseName of less than three (3) characters or begin with a '.' character. Checks for that would need to be added.

What is your plan to deal with filenames that might become duplicate?

$n = 2
Get-ChildItem -File 'G:\TXT\' |
    ForEach-Object {
        Rename-Item -Path $_ -NewName ($_.BaseName.Substring(0,$_.BaseName.Length - $n) + $_.Extension) -WhatIf
    }

or, if this is not in a script file...

$n = 2
gci -file G:\TXT|%{rni $_ ($_.BaseName.Substring(0,$_.BaseName.Length-$n)+$_.Extension) -WhatIf}
0
Theo On

To replace filenames for files starting with 'a0', next a single character followed by 'mm.txt' you can do:

Get-ChildItem -Path 'G:\TXT' -Filter 'a0?mm.txt' -File | 
Rename-Item -NewName {'{0}{1}' -f ($_.BaseName -replace '..$'), $_.Extension} -WhatIf

$_.BaseName -replace '..$' strips the last two characters from the file's basename

The above pattern 'a0?mm.txt' does allow to rename also files like 'a0Xmm.txt' or 'a09mm.txt' and if you need to make sure they can only start with 'a01' and 'a02' you need to append an extra Where-Object clause like:

Get-ChildItem -Path 'G:\TXT' -Filter 'a0?mm.txt' -File | Where-Object { $_.Name -match '^a0(1|2)' } |
Rename-Item -NewName {'{0}{1}' -f ($_.BaseName -replace '..$'), $_.Extension} -WhatIf

regex pattern '^a0(1|2)' means the file name must start with 'a0' folowed by either a '1' or a '2'

I have added the -WhatIf safety switch, so no file will actually be renamed and the result of the code will only be visible on screen. Once you are convinced the code does work for you, remove or comment out the -WhatIf switch and run again