Batch SVN Rename

Jon Galloway is my batch file hero.  He’s the one who introduced me to the FOR %%A in ... syntax.

Today I needed to rename a bunch of files.  On one project, we haven’t kept our file extensions consistent when creating a stored procedure file in a Database project. Some of them had .prc extensions and others have .sql extensions.

I wanted to rename every file to use the .sql extension.  I couldn’t simply use a batch rename program because I wanted these files renamed within Subversion, which requires running the svn rename command.

So using a batch file Jon sent me, I wrote the following.

FOR %%A in (*.prc) do CALL :Subroutine %%A

GOTO:EOF

:Subroutine
svn rename %~n1.prc %~n1.sql
GOTO:EOF

Pretty nifty.  For each file in the current directory that ends in the .prc extension, I call a subroutine.  That subroutine makes use of the %~n1 argument which provides the filename without the extension.

For help in writing your batch files, type help call in the command prompt.

I can see using this technique all over the place. I will leave it to my buddy Tyler to provide the Powershell version.

What others have said

Requesting Gravatar... aaron Sep 20, 2006 10:40 AM
# re: Batch SVN Rename
or you could just type this in the shell and save some HD space/time:
FOR %A in (*.prc) do svn rename %~nA.prc %~nA.sql
Requesting Gravatar... Haacked Sep 20, 2006 10:52 AM
# re: Batch SVN Rename
Good point Aaron. I was repurposing a file that did a lot more in the Subroutine. Essentially it is a template for doing this sort of thing.

Obviously my example is not a good demonstration of the template.
Requesting Gravatar... bitacle.org Sep 20, 2006 10:53 AM
# Bitacle Blog Search Archive - Batch SVN Rename
[...] Jon Galloway is my batch file hero. [...]
Requesting Gravatar... Jason Haley Sep 20, 2006 3:26 PM
# Interesting Finds: September 20, 2006
Requesting Gravatar... Brent Humphreys Feb 14, 2007 9:34 PM
# re: Batch SVN Rename
Man, that is exactly what I wanted to do. Right down to the extensions.

Thanks.... and Google rocks.
Requesting Gravatar... Keith Jun 28, 2010 4:43 PM
# re: Batch SVN Rename
Thanks for the info.

To do a rename recursively I used:

FOR /r %A in (*.prc) do svn rename "%~fA" "%~dpnA.sql"

or if used in a batch file:

FOR /r %%A in (*.prc) do svn rename "%%~fA" "%%~dpnA.sql"

What do you have to say?

(will show your gravatar)
Please add 1 and 6 and type the answer here: