Remove Last Few Characters from Multiple Files at Once [Windows]

A while back, I wrote a post about how to remove the first few characters from multiple files at once in Windows by using a simple command. The solution still works like a charm.

But recently, a person commented if there is a solution that allows to remove the last few characters from multiple files.

A comment about removing last few characters from multiple files at once in Windows

I began experimenting, and it wasn’t working because the filename extension was getting removed as well.

But finally, I have found the solution. Let’s take a look…

How to remove the last few characters from multiple files at once in Windows

I have broken down everything into a few simple steps that are easy to follow:

Step 1 – Open the destination folder

First, open the folder where all those files are located.

Press and hold the button shift and then right-click anywhere in the folder and select Open PowerShell window here option.

A new window will pop open and then proceed to the next step.

I am on Windows 11, you might see CMD or Command Prompt instead of PowerShell if you’re on older Windows versions.

Step 2 – Run the command

Before running the command, make sure what types of files you would be renaming because you will need to modify the command as per the file extension. For example, your file could be .txt, .html, .mp4, .pdf, etc.

Also, count the number of characters that you would like to remove from the right side (excluding the file extension characters). Please note that the dashes or spaces are also counted as characters.

For example, if I have the following files:

  • january1001.pdf
  • feb1002.pdf
  • march1003.pdf
  • april1004.pdf

And I have to remove the numbers at the end of the files, then the file extension here would be .pdf and the number of characters to remove would be 4.

It means, you need to know the following 2 things:

  • The file extension of the files
  • Number of characters to trim from the end of each file

Now, let’s look at the command:

get-childitem *.txt | rename-item -newname { $_.basename.substring(0,$_.basename.length-5) + $_.extension }

Copy-paste the command in PowerShell and hit Enter, and within seconds, all your files will be renamed.

Assumption: I have multiple text files (.txt) and I want to remove the last 5 characters from the filenames. And then only the above command would work.

How the command looks in the Windows PowerShell

Example

Say, you have multiple PDF files and you want to remove the last 7 characters from all the files, then the command would look something like this:

get-childitem *.pdf | rename-item -newname { $_.basename.substring(0,$_.basename.length-7) + $_.extension }

Hit enter, and you’re done!

Related: Remove the First Few Characters from Multiple Files at Once in Windows

Please note that this process is irreversible, so I would recommend that you copy all the files to another folder and then try running these commands to avoid any mistakes.

Also, this command works only for the same type of file i.e. files having the same extension. If you have to rename multiple files of different extensions, you can repeat the process for each type of file.

If you get stuck somewhere, feel free to let me know in the comments below.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

4 Comments

  1. A pretty awesome article. Thanks for sharing! Just to make it more challenging, what if you want to remove irregular length of the last few characters? E.g.
    AB.14 Joe Smith
    AB.132 Amanda Tyler
    AB.58 Martin Johnson

    So that they become
    AB.14
    AB.132
    AB.58

    1. DeepakNess says:

      Glad you found the post useful, Sandy.

      Unfortunately, I haven’t figured that out yet. But if I do, I will be writing about that.

  2. Thanks for this, 2023 and random website helped me out of a bad rename and convert of thousands of files with Irfan View’s relatively archaic tagging. Had to remove the manual period as its $O for extension adding added its own period.

    1. DeepakNess says:

      Glad it helped, Eyo.