In XP, how do I rename a directory of files using a regex or similar to remove part of a filename and add a prefix?

I have a list of files generated from SSMS in XP like this:

dbo.mysproc1.StoredProcedure.sql
dbo.mysproc2.StoredProcedure.sql
dbo.mySproc3.StoredProcedure.sql
... you get the drift

I want to rename them to:

proc.dbo.mysproc1.sql
proc.dbo.mysproc2.sql
proc.dbo.mySproc3.sql
... you get the drift

I'm open to powershell, plain old batch, or whatever. If you can do it with a regex that would be fascinating, but whatever works there too. I want the ends, not the means, yeah? Just not sure what command to use. I do not, however, want to download a tool to do this. Native XP SP3 please.

This is a one-off job, not something to be scripted, if that matters. But I may repeat it in the future, so scriptable is not a bad thing.


Solution 1:

Put this in a batch file that's in the same directory and run:

for /f "delims=. tokens=1,2,3,4" %%A IN ('dir /b *.sql') DO RENAME %%A.%%B.%%C.%%D proc.%%A.%%B.%%D

That'll split the filename up by the "." character using the %%A through %%D variables, and then rename using the rearranged filename.

Solution 2:

A crude PS script for kicks:

gci | foreach { $f = $_; $split = $f.Name.Split("."); $n = [string]::join(".", "proc", $split[0], $split[1], "sql")); ren $f $n;}