4

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.

csg
  • 179
  • 3
  • 4
  • 12

3 Answers3

4
@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
gm2
  • 796
2

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"
dbenham
  • 11,794
1

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.