Batch renaming files

Hello everyone,
In Windows 10. . . I need to remove the first 4 characters from about 250 file names.  They look like this: [ 005_this is a squirrel.jpg ].  I want it to read [ this is a squirrel.jpg ]

I can use CMD and I'm just starting with Powershell.

Thanks so much in advance.
elgin
elgin229Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Here's a small BAT script that you could try to get the job done.  It assumes all the JPG files in the current folder need to be renamed.  Save the BAT, CD to the folder with the files, and run the BAT.  It's in test mode, and will just echo the REN commands it would execute to the command console.  If you like what that look like remove the ECHO on the REN line and run for real.

@echo off
setlocal EnableDelayedExpansion

for %%A in ("%.jpg") do (
    set name=%%~nxA
    set name=!name:~4!
    ECHO ren "%%~A" "!name!"
)

Open in new window


»bp
oBdACommented:
PowerShell; this will only rename .jpg files that actually start with digits, followed by an underscore. It's in test mode and will only display what it would do; remove the -WhatIf at the end to run it for real.
It first gets all jpg files in the directory specified, then excludes all items where the name does not start with digits and an underscore, then renames to the part after the underscore.
The -NewName argument for Rename-Item is in this case a scriptblock where the new name will be constructed dynamically from the element passed in the pipeline ($_).
The short version, how you might enter it in a PS prompt, using aliases:
gci C:\Temp *.jpg | ? {$_.Name -match '^\d+_'} | ren -new {"$($_.BaseName.Split('_', 2)[1])$($_.Extension)"} -WhatIf

Open in new window

In a script, though, aliases should be avoided, and named arguments should be used, so here's the full version:
GetChildItem -Path C:\temp -Filter *.jpg | Where-Object {$_.Name -match '^\d+_'} | Rename-Item -NewName {"$($_.BaseName.Split('_', 2)[1])$($_.Extension)"} -WhatIf

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike SunSenior Systems Engineer (IBM - retired)Commented:
There are also some small 3rd party tools as an alternative such as Lupus Rename
elgin229Author Commented:
I really appreciate these responses.  Because of my limited knowledge, I was able to get the answer from oBdA to work so I'm grateful for that, but also appreciative for the other responses.

I downloaded and installed the Lupus Rename program and was able to get the files renamed, but couldn't figure out how to actually save the changes.  I'm sure it's simple.

Nevertheless, I got the files renamed and I'm happy,

Thank you, gentlemen, for your assistance.

Kindly,
elgin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.