Create Folder Structure Based on Windows Directory

This utility allows you to create a new folder structure within DOORS that is based on the structure of a selected Windows directory.

The dialog allows you to select a DOORS folder and a windows directory. The new hierarchy of folders are created in DOORS below the selected folder that reflects the structure of the selected windows directory.

//  Create Doors Folder Structure from Windows Directory Structure

/*

	...or create Doors from Windows!

   Tony Goodman

*/

pragma runLim, 0

DB   dbMain = null
DBE dbeRootDoorsFolder = null
DBE dbeRootWindowsDirectory = null
DBE dbeBrowseDoors = null
DBE dbeBrowseWindows = null

/*******************
	getWindowsFolder
********************/
string getWindowsFolder(string &dirPath)
{
	OleAutoObj  objDialog     = null
	OleAutoObj  objFolder     = null
	OleAutoObj  objFolderItem = null
	OleAutoArgs args          = create
	string      folderPath    = ""
	string      res           = ""

	dirPath = ""

	objDialog = oleCreateAutoObject("Shell.Application")

	if (null objDialog)
	{
		return("Failed to create dialog object")
	}

	put(args, 0)                         // always zero
	put(args, "Please select a folder")  // title
	put(args, 0)                         // options

	res = oleMethod(objDialog, "BrowseForFolder", args, objFolder)

	if (null objFolder)
	{
		if (res "" != "")
		{
			return("Failed to launch browser: " res)
		}
		else
		{
			return("")
		}
	}

	res = oleGet(objFolder, "Self", objFolderItem)

	if (null objFolderItem)
	{
		return("Failed to get folder item: " res)
	}

	oleGet(objFolderItem, "Path", folderPath)

	dirPath = folderPath

	return(res)
}

/*********************
	isDirectory

	Returns TRUE if string parameter is a valid windows directory.
*********************/
bool isDirectory(string dn)
{
	Stat s = create dn

	if (null s) return false

	if (directory s)
	{
		delete s
		return true
	}
	delete s
	return false
}

/**************************
	getShortName
**************************/
string getShortName(string dir)
{
	string shortName = ""
	int i = 0

	for (i = length(dir) - 1; i > 0; i--)
	{
		if (dir[i:i] == "\\")
		break
	}

	if (i > 0)
	{
		shortName = dir[i+1:]
	}

	return(shortName)
}

/**************************
	createFoldersFromDirectories
**************************/
void createFoldersFromDirectories(string doorsFolder, string windowsDirectory)
{
	string res = ""
    string f = ""
	string shortName = ""
	Folder newFolder = null

	(current FolderRef__) = folder(doorsFolder)

	shortName = getShortName(windowsDirectory)

	if (shortName == "")
	{
		infoBox("Invalid directory name: " windowsDirectory)
		return
	}

	// create DOORS folder
	res = create(shortName, "", newFolder)

	if (res "" != "")
	{
		infoBox(res)
		return
	}

	for f in directory windowsDirectory do
	{
		// ignore this directory and parent
		if (f == "." || f == "..") continue

		if (isDirectory(windowsDirectory "\\" f))
		{
			createFoldersFromDirectories(fullName(newFolder), windowsDirectory "\\" f)
		}
    }
}

/**************************
	doBrowseWindows
**************************/
void doBrowseWindows(DBE dbe)
{
	string res = ""
	string dirName = ""

	res = getWindowsFolder(dirName)

	if (res == "")
	{
		set(dbeRootWindowsDirectory, dirName)
	}
	else
	{
		infoBox(res)
	}
}

/******************************************************************************
	doBrowseDoors

******************************************************************************/
void doBrowseDoors(DBE dbe)
{
    string szFolderName = fnMiniExplorer(dbMain, (current Folder), MINI_EXP_FP, "Select Folder", "Select Folder")

    // ensure that mini explorer has returned a valid module name
    if (szFolderName != "" && folder(szFolderName))
    {
        set(dbeRootDoorsFolder, szFolderName )
    }
}

/**********************
	doOK
***********************/
void doOK(DB db)
{
	string rootWindows = ""
	string rootDoors = ""

	rootDoors = get(dbeRootDoorsFolder)
	rootWindows = get(dbeRootWindowsDirectory)

	if (!confirm("You are about to create a new folder structure.\nAre you sure you want to continue?"))
	{
		return
	}

	createFoldersFromDirectories(rootDoors, rootWindows)

	refreshDBExplorer()
}

/*********************
	MAIN
**********************/
dbMain = create("Create Folder Structure", styleCentered)

label(dbMain, "This utility will create a new folder structure in DOORS " //-
                    "that reflects the structure of the selected windows directory.\n")
dbeRootDoorsFolder = field(dbMain, "Current Folder:", fullName(current Folder), 60, false)
beside(dbMain)
dbeBrowseDoors = button(dbMain, "Browse...", doBrowseDoors)
left(dbMain)
dbeRootWindowsDirectory = field(dbMain, "Windows Directory:", "", 60, false)
beside(dbMain)
dbeBrowseWindows = button(dbMain, "Browse...", doBrowseWindows)

ok(dbMain, doOK)

realize dbMain

show(dbMain)