Wednesday, October 28, 2009

Recursively Delete Subversion Folders



I like to script things like this when I can, for many reasons. First and foremost, I prefer to write programs that do repetitive work for me instead of doing the repetitive work myself. Second, writing little helper scripts like this allows me to share them with my team, boosting their productivity as well. Lastly, as an exercise for me to learn and practice the obscure art of Windows batch scripts. Here is the command as it would be run from a prompt in the root directory where you want to delete .svn directories:
for /r %R in (.svn) do if exist %R (rd /s /q "%R")
Let me break this down. The command starts with for /r %R, which is the recursive form of for. It will recurse through the directory tree, starting in the current directory. In each directory visited, the for variable %R will be set to the for command’s current directory and the rest of the command will then be executed. The next part of the command is in (.svn). This part of the command specifies the patterns that will be matched in each directory visited by for /r. The thing to be aware of here is that the pattern will be repeated in every directory whether or not such a file or folder actually exists. You could just brute-force your way through the folder tree and try removing the .svn directory in every subdirectory, but the output of that would be cluttered with error messages stating that the .svn directory does not exist. Therefore, the next part of the command is an existence check: if exist %R. Just like it sounds, this will only evaluate to true when the directory contains a file or folder named .svn. Finally, the command to remove the directory rd /s /q “%R”. The /s parameter makes the command recursive, and the /q parameter suppresses confirmations.
As handy as this command is, it would be even handier and easier to share if it was a batch script. Here are the modifications necessary to make the command run in a .bat file:
for /r %1 %%R in (.svn) do if exist "%%R" (rd /s /q "%%R")
The first difference you will notice is the presence of a new variable, %1. This variable represents the first parameter passed to the batch file. For this script, the first parameter is the root directory where we want to recursively delete .svn directories. I’ll explain the usefulness of that in a moment. The second change is that all of the references to the for variable %R are renamed to %%R. This is a necessary modification thanks to the way that for command variables are parsed inside of batch files. Let’s create a new file, paste the above command into it, and name it delsvn.bat.
Now that we have this command in a batch file, there are three ways to use it. The first is to call it via command line e.g.
delsvn.bat "drive:\path\"
The second way is to keep the batch file nearby where there are directories that need to have .svn folders removed and drag and drop the folder onto the batch file. This will make the dragged folder’s path be the first parameter of the batch file.
The final way, which makes it easy to invoke the batch file from anywhere, is to add a shortcut to the batch file to the Send To menu. Once the batch file is added there, you can right-click on any directory, choose Send To, then choose the shortcut to the batch file. Just as with the second option, when you send a folder to the shortcut, it will make the folder’s path be the batch file’s first parameter.
There is actually one more way I can think of to re-use this script, and that is to add it as a context menu to directories by modifying the Registry. This is definitely a viable option, but it is a little more involved and also involves a little bit of risk unless you know what you’re doing. Unless you are comfortable hacking around in the Registry, I would not recommend this option.

No comments:

Post a Comment