Enhanced makeDir Function

Wrapper for mkdir that allows you to create a whole new path, not just a single directory. It also traps and reports errors generated by mkdir.

This is more useful than the builtin mkdir function that will throw a runtime error if the full path to the new directory does not already exist.

Usage:

string makeDir(string dirName)

Example:

string errors = ""
errors = makeDir("C:\\Temp\\tom\\dick\\harry")
print errors "\n"

Source code:

//  makeDir

/*
	Wrapper for mkdir that allows you to create a whole new path, not just
	a single directory.

	Usage:

		string makeDir(string dirName)

	Tony Goodman  23 June 2005
*/

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

	Returns TRUE if string parameter is a valid 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
}

/************************************
	getParentDirectory

	Returns parent directory of the given file or directory name.
*************************************/
string getParentDirectory(string fname)
{
    int i = (length fname) - 1

    while (i > 0)
    {
		if ((fname[i] == '\\') || (fname[i] == '/'))
		{
		    return fname[0:i - 1]
    	}

	    i--
    }

    return ""
}

/*************************************
	makeDir

	Creates a new directory path dPath.

	On success returns an empty string.
	On error returns an error message.
***************************************/
string makeDir(string dPath)
{
	string res     = ""
	string dParent = ""  

	// see if the directory already exists
	if (isDirectory(dPath))
	{
		return("Directory already exists!")
	}

	// get name of parent directory
	dParent = getParentDirectory(dPath)

	// check to see if parent exists
	if ((!isDirectory(dParent)) && (!null dParent))
	{
		// recurse
		res = makeDir(dParent)

	}

	if ((isDirectory(dParent)) || (dParent == null))
	{
		// parent directory exists so we can create dPath using mkdir
		// trap errors
		noError

		mkdir(dPath)

		res = lastError
	}

	return(res)
}