Displaying the sizes and dates of files in the server



Have you wished that the output of the dir command from tf.exe would show you the dates and sizes of the files like cmd.exe’s dir command?  Even though tf.exe won’t do that in version 1, the version control API provides the call necessary.  This little app is similar to my last post on displaying labels on a file.  You can find a simple example of creating a workspace, pending changes, and so forth in my September API example.

The GetItems() method is the key part of the app.  Here’s the declaration on the VersionControlServer class.

public ItemSet GetItems(String path, RecursionType recursion)

For this overload of the GetItems() method, there’s no version parameter, so it defaults to get the Item objects for the latest version in the repository.  There is another overload that takes the version, and you could pass “new WorkspaceVersionSpec(wsInfo.Name, wsInfo.OwnerName)” as the version to get the Item objects corresponding to the versions in the workspace.

The first argument is the path on which we want data.  That can be either a local path or a server path.

The recursion parameter specifies how deep we want to go.  If the path is a directory, specifying RecursionType.OneLevel retrieves only the items directly contained in that directory, just like specifying the wildcard “*” would.  If the path is a file, there is no difference between one level of recursion and none because a file can’t have children.  The default for this app is onle level of recursion, which is the same as what both cmd.exe’s dir command and tf.exe’s dir command use.

If the user specifies /r, the app passes RecursionType.Full.  For a directory, that will return all of its descendants.  If the path doesn’t match a directory, the server will recursively find all items under path that match.  The path it uses is the directory portion of the path, and the pattern is the file name portion of the path.  So, if the user specifies “c:\project\readme.txt /r” the server will return all files called readme.txt underneath c:\project, recursively.

In the app, we only want the items for display, which is the Items property on the ItemSet object returned by GetItems().  The other two properties on ItemSet, QueryPath and Pattern, indicate how the server used the specified path to do the matching.  In our previous example of “c:\project\readme.txt /r” the result would be QueryPath set to the server path corresponding to c:\project and Pattern set to readme.txt.

Here’s an example of running the app.

D:\ws1>D:\code\projects\DirSize\DirSize\bin\Debug\DirSize.exe . /r
10/25/2005 11:40:25 AM    <DIR>             $/testproj
10/25/2005 03:59:50 PM    <DIR>             $/testproj/A
10/25/2005 03:59:50 PM    <DIR>             $/testproj/B
10/26/2005 11:04:34 PM                 7996 $/testproj/out.txt
10/25/2005 04:55:50 PM                    6 $/testproj/A/a.txt

2 files, 3 folders, 8002 bytes

To build it, you can create a Windows console app in Visual Studio, drop this code into it, and add the following references to the VS project.


[Update 11/4/2005] I added some explanation of the version used with the GetItems() call and the HashValue property of the Item class.

Buck Hodges

Follow Buck   


    Leave a comment