Enumerating the # and names of files in a VSS folder

A customer asked me this question via email today. Unable to remember how to do this (I’ve been off VSS for over a year), I pinged one of the VSS team’s awesome developers, Alin, who promptly responded with this gem:

“You can use VSS Automation to find out the number of files in a folder and their names. Here is the function for doing that:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim db As New SourceSafeTypeLib.VSSDatabase

        db.Open(“\\alinc00\vss$\”, “Guest”, “”)

        Dim item As SourceSafeTypeLib.VSSItem

        item = db.VSSItem(“$/”)

        MsgBox(“Total number of files and subfolders = “ + item.Items.Count.ToString)

        Dim files As Integer

        Dim child As SourceSafeTypeLib.VSSItem

        files = 0

        For Each child In item.Items

            If child.Type = SourceSafeTypeLib.VSSItemType.VSSITEM_FILE Then

                files = files + 1

            End If


        MsgBox(“Total number of files = “ + files.ToString)

    End Sub

The command line SS.exe alone cannot be used, but combined with other utilities it can provide the answer.

ss dir $/ | findstr /R “^[^\$]” | wc –l

(ss dir lists the files and folders, findstr filters out files, wc counts the lines)

Subtract 1 from the answer to find the number of files (because the last line displayed by ss.exe contains the total number of files and folders)  or use something like

@for /F %I in (‘ss dir $/ ^| findstr /R “^[^\$]” ^| wc -l’) do set /a FILES=%I-1 > NUL

@echo There are %FILES% files in this folder

Comments (1)

  1. Baarish says:

    I am trying to calculate the size of folders placed under VSS.

    While I am able to calculate the folder sizes recursively for Directories & SubDirectories placed in local drive, if I try to use the same approach to calculate folder sizes in VSS I get the following error:

    Exception :An unhandled exception of type ‘System.IO.DirectoryNotFoundException’ occurred in mscorlib.dll

    Additional information: Could not find a part of the path "\SourceProject1Testing".

    Following is the code:

    Imports SourceSafeTypeLib

    Imports System.IO

    Form Load()

    Dim db As New SourceSafeTypeLib.VSSDatabase

    db.Open("\SourceProject1", "ani", "abc12$$")

    Dim item As SourceSafeTypeLib.VSSItem

    item = db.VSSItem("$/Testing")

    MsgBox("Total number of files and subfolders = " + item.Items.Count.ToString)

    Dim size As Long

    Dim d As New DirectoryInfo("\SourceProject1" & "" & item.Name)

    size = DirSize(d)

    End Sub

    Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long

    Dim Size As Long = 0

    ‘ Add file sizes.

    Dim fis As FileInfo() = d.GetFiles() ‘Throws Exception

    Dim fi As FileInfo

    For Each fi In fis

    Size += fi.Length

    Next fi

    ‘ Add subdirectory sizes.

    Dim dis As DirectoryInfo() = d.GetDirectories()

    Dim di As DirectoryInfo

    For Each di In dis

    Size += DirSize(di) ‘To get direstories/files under current directory

    Next di

    Return Size

    End Function ‘DirSize

    The VSS is on the local machine.Is there any API through which I can get the physical path to VSS database..so that I can access all the working folders under it, and thereby calulate the folder size.

    Your help in this regard would be highly appreciated.

    Thanking in advance.