Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to batch rename files in a folder containing files in the following name pattern:

xxxtemplatexxxxx.xlsx

One example of the name I'd have is:

IDA_template - differentwordingshere.xlsx

The x's may not be the same. I want to replace template with the date, for example 201307.

I tried

ren *template* *201307*

But it appended 201307 to the end instead.

In bash I could probably do

rename template 201307 *.xlsx

but unfortunately this will be on Windows.

.bat file will be placed in the same folder as where the files are.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted
@echo off
setlocal enabledelayedexpansion

for %%F in (*template*) do (
    set name=%%~nxF
    set name=!name:template=%date:~-4%%date:~-10,2%!
    rename "%%~fF" "!name!"
)

exit /B 0
share|improve this answer
    
Thank you! I managed to make minor changes to get the date format I need. Question though, what is the difference between %%F and %%~nxF? When I inserted echos in the code both seems to return the same thing. Also, how does the second set work? I tried replicating that with set foo=quick brown fox || set foo=!foo:brown=black! || echo %foo% And I got !foo:brown=black! as the output. –  csg Jul 29 '13 at 1:39
    
Ah. Found out that it is due to the delayed expansion. Also managed to make some more changes to allow custom search and replace with extension filtering. Not perfect - as it can cause renaming some files more than once (ie. when the first file is renamed and sorted to the bottom, the for loop will go through it again) - but for my usage I probably won't encounter that case. Thank you! –  csg Jul 29 '13 at 2:50
add comment

If you install PowerShell you could use something like:

get-childitem *.xlsx | foreach { rename-item $_ $_.Name.Replace("template", (Get-Date -format "yyyyMM")) }

This will rename all the XLSX files in the current folder, replacing instances of the word "template" in the file name with the current Year and Month.

share|improve this answer
    
Thanks! I actually do have PowerShell installed and that code works great! However when I save it as a .ps1 file through Notepad, PowerShell window flashes but doesn't do anything - am I doing something wrong? –  csg Jul 28 '13 at 3:47
    
Tried to create a batch file with this code: <code>powershell -Command "& {get-childitem *.xlsx | foreach { rename-item $_ $_.Name.Replace("template", (Get-Date -format "yyyyMM")) }}"</code> but that did not work either –  csg Jul 28 '13 at 4:12
add comment

The Windows RENAME is quite limited with what it can do with wild cards. It won't help with your current problem, but you can see what is possible at How does the Windows RENAME command interpret wildcards?

I have written a hybrid JScript/batch utility called REPL.BAT that performs a regex search and replace on stdin and writes the result to stdout. The utility is pure native script that works on any Windows version from XP onward. No exe download is required.

REPL.BAT is available here. Full documentation is embedded within the script.

I continue to be amazed how useful REPL.BAT can be. It can easily solve your current problem.

Assuming REPL.BAT is in your current directory, or better yet, somewhere in your PATH, then the following moderately complex one liner will work from the command line:

for /f "tokens=1* delims=?" %A in ('dir /b *TEMPLATE*^|repl "(.*)TEMPLATE(.*)" "$&?$1201307$2" I') do @ren "%A" "%B"

Or as an easier to read batch script:

@echo off
for /f "tokens=1* delims=?" %%A in (
  'dir /b *TEMPLATE*^|repl "(.*)TEMPLATE(.*)" "$&?$1201307$2" I'
) do ren "%%A" "%%B"

I pipe the matching file names to REPL.BAT to modify each name into the original name, a ? delimiter, and then the new name. I chose ? as a delimiter because it cannot appear in a file name. The FOR /F command parses the results and executes a REN command for each file.

The above can easily be modified to recursively rename all matching files in the folder hierarchy:

@echo off
for /f "tokens=1* delims=?" %%A in (
  'dir /b /s *TEMPLATE*^|repl ".*\\(.*)TEMPLATE(.*)" "$&?$1201307$2" I'
) do ren "%%A" "%%B"

Finally, it might be nice to dynamically establish the date, but working with date and time in batch is usually a royal pain due to differences in locale.

Another useful hybrid JScript/batch utility called GETTIMESTAMP.BAT provides robust timestamp computation and formatting. It can create a timestamp in virtually any format desired. The date/time can be specified, or it can be gotten from the system clock. It provides for date and time offsets, as well as time zone specifiers both for input and output.

GETTIMESTAMP.BAT is available here.. The utility is at the bottom of the post. Extensive documentation is embedded within the script.

With GETTIMESTAMP.BAT in the PATH, it is now trivial to extend the code above to dynamically establish the date string. This is the recursive folder version:

@echo off
setlocal
call getTimestamp -f {yyyy}{mm} -r dt
for /f "tokens=1* delims=?" %%A in (
  'dir /b /s *TEMPLATE*^|repl ".*\\(.*)TEMPLATE(.*)" "$&?$1%dt%$2" I'
) do ren "%%A" "%%B"
share|improve this answer
    
Hi dbenham, thanks for the response. This is quite a bit over my understanding. I placed REPL.BAT and another .bat file containing the code above, but nothing happens (cmd screen does flash for a second) when I run it. Am I doing something wrong? –  csg Jul 28 '13 at 15:08
    
@csg - I tested the code, so yes, you must be doing something wrong. I suspect that your TEMPLATE is just a placeholder for something else. If so, then you might have to escape some characters so that regex interprets them as literals. If you still need help, then edit your question to show the actual TEMPLATE string to be replaced. –  dbenham Jul 28 '13 at 15:19
    
I have just updated my original question with an example. The automatic timestamp does seem very attractive. However this is really way past my knowledge now - would I be able to -1 off the returned month? Thanks so much dbenham. –  csg Jul 28 '13 at 15:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.