Remove First Few Characters from Multiple Files at Once [Windows]
I had 78 markdown files (.md) in 2021-04-25-rename-multiple-files.md
format and I wanted to remove the date from all the files at once. And, the final format should be like rename-multiple-files.md
format.
Renaming all the files one-by-one manually would take ages, and after researching a bit I found the perfect solution that I needed.
I was able to remove the first 11 characters (to remove the date from the filenames) from all the 78 .md files in the folder.
Let me explain everything step-by-step:
How to remove the first few characters from multiple files at once in Windows
I might not be able to describe properly, but it’s actually very easy.
Step 1 – Open the destination folder
First, open the folder where 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’m on Windows 10 but for some older Windows, you might see CMD or Command Prompt instead of PowerShell.
Step 2 – Run the Command
Before running the command, it’s important to know what type of files you would be renaming – is it a .txt file? .mp4, .md, .docx, or any other.
Also, you need to count the number of characters you would remove from the left side. Please note that a dash or space in the filename would also be counted as 1 character.
Now that you know:
- file extension (.mp4, or .txt, .docx, or any other)
- the number of characters to trim from each file
It’s time to run the following command (but do not forget to edit before running):
get-childitem *.txt | rename-item -newname { [string]($_.name).substring(5) }
Assumption: If you have multiple text files (.txt) and want to remove the first 5 characters from the filename, then only the above command will work.
Example
Let’s assume you have multiple image files (.jpeg) in the folder and you want to remove the first 2 characters from the start then the command would be:
get-childitem *.jpeg | rename-item -newname { [string]($_.name).substring(2) }
And, that’s it.
Within seconds, all your files will be renamed.
Related: Remove the Last 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, please note that this trick will work for files of the same extension. If you have to rename multiple files, you can repeat the process for each type of files.
If you get stuck somewhere, please let me know in the comments below.
Thanks and appreciation Deepak!
Glad it helped, Kamal.
Super easy. Clear instructions that worked. Renamed a couple hundred files in seconds. Thanks!
Thanks, Emily. Glad that it worked for you.
This worked amazing, thank you so much!
Is there a similar way to remove “x” number of characters from the end of the file name? Or search and replace in Windows Explorer in a similar way that Microsoft Excel does?
Hi Luke, glad it worked for you.
I haven’t yet figured out how to remove chars from the end but I’ll update as soon as I do.
Thank you.
Get-ChildItem *.mp4 | rename-item -NewName {$_.name.substring(0,$_.BaseName.length-63) + $_.Extension -replace “_”,” “}
This code works by replacing the characters counting from the end backwards
i dont take credit though i found this on a subreddit
Replace the “63” with however many characters you wish to remove.
I also changed the file type to mp4 here so change it back of you wish to do other files
What we do if want to remove from right side characters and rename file
Instead of renaming based on # characters you want to remove, is there a way to do it based on everything preceding a particular character?
For example, I want to remove all characters proceeding the underscore
1_filename
2_filenameX
10_filenameY
End result
_filename
_filenameX
_filenameY
Yes, that would be even better but I haven’t figured that out yet.
Does anyone else have any idea how to do it? 👆
Thank you, this worked well for me. I renamed over 3000 files at once this way.
Glad it worked for you.
Is there a similar way of doing this for folders?
Super helpful! Adding to my repertoire of helpful time s-saving skills. Should probably look into learning how to use PowerShell a lot more!
Glad it helped, Barry. Definitely, PowerShell is amazing.
Hi There,
I used the same script you shared previously, and I found out that to be able to maintain characters in the middle you just have to know the start position and the end position, in my case, I had 300 files starting like XXXXXXXXX20210908YYYYYYYYY, I wanted to rename them as the date only, so I started in the position 9 and kept 8 characters, the result was that the new name files were only dates. You can apply the same principle to erase the last characters by starting in the 0 position.
get-childitem *.txt | rename-item -newname {[string]($_.name).substring(9,8)}
I hope this explanation works well for you.
thanks
That’s great! Thanks for sharing, LuisPuma.
Great, many thanks 👍👍
This variation will trim your filename (PDF in this case to 17 characters) while adding the extension.
get-childitem *.pdf | rename-item -newname {[string]($_.name).substring(0,17) + $_.Extension }
Hi,
If I want to remove the required number of characters from a multiple folder, how can we do that?
I used below command. but its not working. Please help me on this
get-childitem *.dir | rename-item -newname { [string]($_.name).substring(1) }
Is there a command that will allow me to do the but instead of the first 5 characters I can remove the last five characters?
Example:
Original file name: Apple_12345.pdf
Want to change file name to: Apple.pdf
Hi Johnathan,
Yes, it works. I just wrote another post on how to remove the last few characters from multiple files that you can check out.
Hope it helps. Thanks.
Hey is there a way I can do this for multiple folders in a folder at once? to explain a long time ago back when ipods were still huge i had to change computers and used the ipod as the music harddrive and pulled all the music off the ipod harddrive but they all ended up with a .filename.(insert music type) so it’s been years at this point but is there a way to select the music folder and affect the subfolders within it?
Hey John, I understood your use case but haven’t figured out a way to rename files in multiple folders at once. As of now, you will have to do this for each folder one by one.
From what I understand, couldn’t you use a combination of cmd and this script to mass rename files via echo?
Haven’t tried that! Did it work for you?
Thanks so much, you just saved me so much time!! The instructions were super easy to follow even for someone like me, who isn’t very well-versed in stuff like this 🙂
Glad it helped, Calico.
Thank you! Most helpful article I’ve found so far on this.
I’m trying to remove a single character that appears in every file. I’m trying to remove the “.” That appears in ‘Wausau.JFK”
What code would work for this?
Thanks so much!
Hey Scott,
get-childitem *.JFK | rename-item -newname { ($_.Name -replace '[.]', '') }
The above command can help you remove the “.” from all the files with “JFK” extension. But make sure you have a copy of all the files before you run the command because the process is irreversible.
Let me know if it helps.
Saved me a couple hours!!
Thank you Deepak! Appreciate Ya
it is life saving👍
Glad it helped you. 🤩
Pretty slick!
Glad it helped.
THANK YOUUUUU!
Glad it helped!
Thank you it worked
Great! Thank you so much!