Jeremy Sheeley

Secrets from the Vault

Searching through the repository and checking in files.

One of the most common things that people ask for in Vault is a way to find every file in their repository that matches some pattern and do something. Some of the requests are:
  • Search for a file based on name to see if it has already been added to the repository.
  • Search for any files named AssemblyInfo.cs, check them out, edit them and check them in.
I'll be showing you how to do the second, since lots of people have questions on the best way to edit a file and check it in. Once again, we'll be modifying the Vault client template that we created a long time ago. You can find the complete project here.

Searching through the repository

The first thing that we need to do is find the topmost node that we want to search. In most cases, you don't want the top of your search to be $/, because then you will be finding any AssemblyInfo.cs files in branches, etc. That's where the handy "FindFolderRecursive" function comes in. Also note that there is a corresponding FindFileRecursive function.

VaultClientFolder searchFolder = myClient.Repository.Root.FindFolderRecursive(searchRoot);

FindFolderRecursive looks through the repository to find a folder with the path that you give it. Once you get a VaultClientFolder, there are a few interesting things you can do to it, such as pass it as an argument to ClientInstance.Get, ClientInstance.CheckOut, etc. For this example, we're going to walk down the folder looking for files with a certain name. I do this with a function called RecursivelySearchForFileName.

public static void RecursivelySearchForFileName(VaultClientFolder folder, string fileName, ref ArrayList returnArray)
{
if (folder.Files != null)
{
foreach (VaultClientFile subfile in folder.Files)
{
if (subfile.Name == fileName)
{
returnArray.Add(subfile);
break;
}
}
}
if (folder.Folders != null)
{
foreach (VaultClientFolder subfolder in folder.Folders)
{
RecursivelySearchForFileName(subfolder, fileName, ref returnArray);
}
}
}

Get the files, Modify the files, Check in the files

Once we have the array of VaultClientFile, we need to check them out and get them

myClient.CheckOut((VaultClientFile[])returnArray.ToArray(typeof(VaultClientFile)), 2 /*request exclusive checkout. Use 1 for non-exclusive*/, "Checkout Comment");
myClient.Get((VaultClientFile[])returnArray.ToArray(typeof(VaultClientFile)), true, MakeWritableType.MakeAllFilesWritable, SetFileTimeType.Current, MergeType.OverwriteWorkingCopy, null);
myClient.Refresh();

I'm going to skip the actual editing of the files here, but the key from the Vault side of things is using the following line to get the path on disk that corresponds to the VaultClientFile.

string diskFile = myClient.TreeCache.PhysicalPath(foundFile);

Now we can use myClient.Commit() to commit the changes. Now you should be able to write your own API programs to edit files.

Comments

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 5:45 AM

Tilman said:

Question: Why do you do a Get after you check out files?

# June 19, 2007 5:50 AM

Michael said:

I'd guess the a "checkout" only marks the files as checked-out. It doesn't actually retrieve them.

# December 11, 2008 4:44 PM

Matt said:

Have you ever thought it would be nice to filter their edited files by the name?

# February 18, 2009 5:01 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)