• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

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
  • 3
1 Solution
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

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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now