Bat file to rename files

Need to create a bat file that renames/copies all files in a folder containing -A to the same file name in the same folder without the -A.  Also need to overwrite the file that is already there.  Also need to keep the original -A file
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Mohamed MagdyCTOCommented:
Is it necessary to make it from a bat file? Couldn't you use a program like Bulk Rename Utility?
Steve KnightIT ConsultancyCommented:
Try this if you want batch.  Remove the capitalised word ECHO to have it copy.  At the moment it just shows you the commands.


@echo off
setlocal enabledelayedexpansion
set MyDir=c:\test
pushd %mydir%
for /f "tokens=*" %%a in ('dir /b /a-d *-A*.*') do (
  set newname=%%~a
  set newname=!newname:-A=!
  echo Working on %%~a, new name is !newname!
  ECHO copy /Y "%%~a" "!newname!"

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
Steve KnightIT ConsultancyCommented:
To explain if you want to know:

2: Enable delayed expansion.  Without then setting "newname=" won't work as we want in the loop
3/4: Define where you want to run the file changing, PUSHD changes to that even if it is a UNC path etc.
5: Loop through the output of dir command showing all files with -A in the name putting each name in %%a
6/7:  Set newname to the name found, then remove the -A and replace it with blank
8: show you what it is going to do
9: Copy and overwrite the one name with the other.  If the name doesn't exist will just create.
10: Finish the looo
11: Put you back in the dir you came from.

Steve KnightIT ConsultancyCommented:
That worked ok then?
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
Microsoft DOS

From novice to tech pro — start learning today.