Avatar of shragi
shragi
Flag for India asked on

rename multiple files using command prompt

general command to rename extensions of multiple files using command prompt in windows is
ren *.html *.txt

but if i want to rename a file from something like below to
ABC_4739874_2349827.txt
to
ABC_4739874_2349827_RENAMED.txt

how can i do that.

Thanks
Windows OSProgrammingShell Scripting

Avatar of undefined
Last Comment
NVIT

8/22/2022 - Mon
NVIT

for %A in (*.txt *.html) do ren %~nA_RENAMED%~xA
Joseph Daly

Using powershell.

$dir = "C:\filepath"
gci $dir | foreach {
$name = $_.name
$newname = $name+"_renamed"
rename-item $dir\$name $newname
}

Open in new window

Jonathan Brite

You can use wild cards in command line with rename.  I am pretty sure your example would be (without quotes) "rename ABC_*.txt ABC_*_RENAMED.txt"  

I also think Josephs PS script would be much cleaner, especially if going through multiple directories)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
shragi

ASKER
@NewVillageIT
I got error saying "the syntax of the command is incorrect"
for %A in (*.txt) do ren %~nA_RENAMED%~xA
Jonathan Brite

have you tried a normal rename command like I listed above yet? (be sure to test it before trying it globally)
NVIT

Correction to my CMD version:
for %A in (*.txt *.html) do ren "%A" "%~nA_RENAMED%~xA"

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
NVIT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
shragi

ASKER
@Jonathan Brite

your command is not working - it is renaming wrongly
ABC_4739874_2349827.txt is renamed to ABC_4739874_RENAMED.txt
but i want it to be renamed to
ABC_4739874_2349827_RENAMED.txt
Jonathan Brite

Thanks NewVillageIT.  That helped me as well.
shragi

ASKER
thanks newvillageIT :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
NVIT

I'm glad it worked for you, shragi, and Jonathan. Have a nice day...8-)