• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 700
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
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

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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