Report the Number of Links Per Linkset

This script loops through the current folder and recursively through subfolders and reports on the linksets it finds. The output is a count of links found in each linkset. Output is to a CSV file.

//  List Link Counts per Linkset

/*
	Recursively scan the current folder or project and generates a
	count of Links per Linkset.

	Output is to a CSV file.

	Tony Goodman.
*/

pragma runLim,0  /* turn off timeout dialog */

DB  dbMain        = null
DBE dbeExport     = null
DBE dbeExportPath = null

Folder currFolder = null
Buffer  outBuf    = null

/************************************
	scanLinkModule
************************************/
void scanLinkModule(Item itm)
{
	Module        lm    = null
	Module        sm    = null
	Object        o     = null
	string        lmn   = ""
	string        src   = ""
	string        tgt   = ""
	int           count = 0
	ModuleVersion mv    = null
	Link          l     = null

	lmn = fullName(itm)

	// read the link module
	lm = read(lmn, false)

	for o in lm do
	{
		// get names of source and target modules
		src = o."source" ""
		tgt = o."target" ""

		// read the source module
		sm = read(src, false)

		filtering off

		for o in all(sm) do
		{
			for l in all(o->lmn) do
			{
				// check for target module for matching name
				mv = targetVersion(l)

				if (fullName(mv) == tgt)
				{
					count++
				}
			}
		}

		close(sm)

		// output count for this linkset
		outBuf += lmn "," src "," tgt "," count "\n"
	}

	close(lm)
}

/************************************
	scanFolder
************************************/
void scanFolder(Folder f)
{
	Item   itm

	if (null f) return

	for itm in f do
	{
		if (null itm) continue
		if (isDeleted(itm)) continue

		if (type(itm) == "Link")
		{
			scanLinkModule(itm)
		}
		else if ((type (itm) == "Project") || (type (itm) == "Folder"))
		{
			scanFolder(folder(itm))
		}
	}
}

/************************************
	doClose
************************************/
void doClose(DB db)
{
	release(dbMain)
}

/************************************
	doList
************************************/
void doList(DB db)
{
	outBuf = create
    outBuf = ""

	string fName = get(dbeExportPath)

	Stream outfile = write(fName)

	outfile << "Link Module, Source Module, Target Module, No.Links\n"

	scanFolder(currFolder)

	outfile << outBuf
	close(outfile)
	delete(outBuf)

	doClose(db)
}

/************************************
	MAIN
************************************/
currFolder = current Folder

dbMain = create("List Link Counts")

label(dbMain, "This utility exports a count of all links per linkset.\n" //-
              "Output is to a CSV file as follows:\n" //-
              "Link Module, Source Module, Target Module, No.Links\n")

dbeExport     = field(dbMain, "Folder", fullName(currFolder), 50, true)
dbeExportPath = fileName(dbMain, "Report File", "C:/Temp/" name(currFolder) " LinkCounts.csv")

ok(dbMain, "OK", doList)
close(dbMain, true, doClose)
realize dbMain

block(dbMain)

destroy(dbMain)
dbMain = null