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

Batch file to rename files

hi -

I have over 50 files in a folder that have the year 2012 in the file name (i.e. xxxxx FY2012 yyyy.xls).  I would like to rename them all to the same name but change 2012 to 2013.

How can this be done using DOS?

Thanks in advance!
0
eklin
Asked:
eklin
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
Bill PrewCommented:
This should work.  Save as a BAT file, then edit the PUSHD to your folder.  Right now it will only echo the REN commands it would do so you can check them.  If it looks good, remove the ECHO in front of  the REN command and run for real.

@echo off
setlocal EnableDelayedExpansion

pushd c:\your\folder

for %%F in (*2012*.xls) do (
  set Name=%%~F
  ECHO ren "%%~F" "!Name:2012=2013!"
)

popd
pause

Open in new window

~bp
0
 
Steve KnightIT ConsultancyCommented:
Open notepad
Paste this code in, Save As.  Choose "all files" as type rather than "text files" and save it as "fixmyfiles.cmd" or whatever.

You amend the folder / old / new lines so it will work next year too...

HTH

Steve

@Echo off
setlocal enabledelayedexpansion
set old=2012
set new=2013
set folder=C:\my folder\name
pushd "%folder%"
  for /f "tokens=*" %%f in ('dir /b /a-d *2012*.xls') do (
  set name=%%~f
  set name=!name:%old%=%new%!
  echo Renaming %%f to !name!
  ren "%%~f" "!name!"
)
popd
endlocal

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Afternoon Bill, looks like I was a little slow there :-)
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Seaton007Commented:
There is also a free utility you can use to do this: http://www.bulkrenameutility.co.uk/Screenshots.php
0
 
eklinAuthor Commented:
Thanks to you both!  I tried Bill's and it works fine.  I'll try to split the points 2/3 Bill and 1/3 Dragonit to be fair later today.  Let me know if that works.  I appreciate your help!  Saved me hours of copy and paste :)
0
 
Steve KnightIT ConsultancyCommented:
No problem either way, basically identical... even down to our variable names, spooky!
0
 
Bill PrewCommented:
Sure, whatever works for you I'm okay with.  Glad you got some help with your need, that's why we come here.

~bp
0
 
eklinAuthor Commented:
Both answers produce the same result, although I found Steve's answer easier to maintain when changing the variables.  Points were awarded based on who provided answer first.  Thank you both!
0
 
Steve KnightIT ConsultancyCommented:
No problem, glad it helped.  Basically both wrote them at the same time and very similar so like you say,

Steve
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now