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?

1

1

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.

jcolebrand

Posted 2010-12-14T21:00:36.883

Reputation: 1 020

Answers

4

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.

Ryan

Posted 2010-12-14T21:00:36.883

Reputation: 206

1holy crap that's awesome. – jcolebrand – 2010-12-14T21:23:47.007

1Ha thanks. As long as the file names are pretty consistent this'll do the trick. – Ryan – 2010-12-14T21:27:56.260

4

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

Nick T

Posted 2010-12-14T21:00:36.883

Reputation: 2 417

oooh, what does GCI do? "Get-ChildItem" nice – jcolebrand – 2010-12-14T21:34:00.373