And then just google a ready made code for renaming files.
Why not just do that for the batch file route? That avoids having to install Perl, worrying about having to learn the code, etc. and so-forth.
The batch file doesn't really have regex, but is there a reason to defer to a full programming language which Greg likely doesn't already have installed?
How many of the files were having duplicate prefixes added?
Would something like this work?
for %%f in (*d2*) do (
if not !str!==01 ren "%%f" "01%%f"
for %%f in (*d#2*) do (
if not !str!==02 ren "%%f" "02%%f"
I'm not sure if you can do substrings on the %%f, but that makes sure that each file only gets the prefix added once.Edit:
Apparently this requires delayed expansion. I've changed the code (tested now lol). I couldn't figure out any way to take a substring from %%f. And I was getting an error trying to use the substring directly in the if statement so that's why I put the additional variable.Edit:
Just wanted to put some info here from PMs between myself and Greg. For anyone unaware batch files treat very few things in a case sensitive manner so any patterns used by this batch file would have to be unique. One other option is that for Greg's use the patterns are always separated by whitespace. So if you have a whitespace character on either side you could make the pattern something such as:
[code]for %%f in ("* d2 *") do ([/code]
enclose this pattern in quotes or it won't read the whitespace as part of the pattern. Just FYI for anyone reading this.