Recursion with configuration files

I came up with this to figure out the name a path to the folder below some arbritrary folder in the path where the current dgn is loading from. My need and testing was with ProjectWise managed workspaces but it should work with regular files and folders as well (but is untested for this). Testing was done with MicroStation Connect Update 16.2 and ProjectWise 10.00.03.299

The configuration in this post solves the issue below, but can probably be generalized to more needs.

issue: given a path with an arbritrary number of subfolders:

pw://server:dsource/Documents/folder1/WorkAreaName/SubProject/SubFolderA/SubFolderB/.../file.dgn

Figure out the name of the folder below WorkAreaName.

Solution:

Microstation and ProjectWise managed workspaces allow for a configuration file to load itself again. Normally you would avoid this because it's an infinite loop, but as long as you have proper testing and error conditions you can avoid that. So my solution takes the current directory, checks if the parent above the current folder matches a specified name, and if not sets the "current folder" name to the parent directory path and calls itself again. In this manner it walks up the directory tree until it either finds a match, has checked all folder names in the path, or (as a failsafe) the loop has executed a certain number of times (20 in this config).

# This config file loops by loading itself over and over. There is a potential
# for an infinite loop if you are not careful!
#
# This config file uses variables prefixed with _FINDSF_ so it does not conflict
# with other configuration files.
#
# Before loading this config set the following variables:
# _FINDSF_CONFIG    => Full path & filename to this configuration file
# _FINDSF_FIND_ROOT => Just the folder name (not the path) of the root folder we're searching for
#
# At finish this config returns:
# _FINDSF_SUBFOLDER_PATH => Full path to first sub-folder under the root.
# _FINDSF_SUBFOLDER_NAME => Just the name of the first sub-folder under the root name.
#
# If the config can't locate ROOT name in the _DGNDIR path it will throw an error, if it
# is unsuccessful for some other reason, the two return variables will remain undefined

%if (! defined(_FINDSF_CONFIG)) || (! defined(_FINDSF_FIND_ROOT))
	%error Both _FINDSF_CONFIG and _FINDSF_FIND_ROOT must be defined
%endif

# First Run Of Config File
%if ! defined(_FINDSF_PROCDIR)
	# if running in ProjectWise some directory operators seem to work on the working
	# directory instead of the URL. This strips the path to just the pw folder names.
	_FINDSF_PROCDIR   = dir(${_DGNDIR})
%endif

# append a ;1 each time config is loaded
_FINDSF_LOOPCOUNT > 1
_FINDSF_PARENTNAME = lastdirpiece(parentdir(${_FINDSF_PROCDIR}))
%if ${_FINDSF_PARENTNAME}==${_FINDSF_FIND_ROOT}
	# Success = PROCDIR is one folder under the ROOT
	# Set the path back to a real path
	_FINDSF_SUBFOLDER_PATH = dev(${_DGNDIR})${_FINDSF_PROCDIR}
	_FINDSF_SUBFOLDER_NAME = lastdirpiece(${_FINDSF_PROCDIR})
	# reset variable so this file can be called again
	%undef _FINDSF_PROCDIR
%else
	# don't go into an infinite loop, 20 loops or until no directories left
	%if (${_FINDSF_PARENTNAME}=="") || ($(_FINDSF_LOOPCOUNT)=="1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1")
		%error Unable to locate work area folder ${_FINDSF_FIND_ROOT} in path ${_DGNDIR}
	%else
		# strip off last directory and loop
		_FINDSF_PROCDIR = parentdir(${_FINDSF_PROCDIR})
		%include $(_FINDSF_CONFIG)
	%endif
%endif

I save that file as FindSubFolder.cfg in a folder name FindSubFolder under the _USTN_CONFIGURATION directory

To use that config file, from another config file i call it with as below. The lastdirpiece(DMS_PROJECT(_DGNDIR)) is the name of the ProjectWise work area the dgn file is in (so in my issue example above it would be WorkAreaName) and it should return the name and path to SubProject folder

_FINDSF_CONFIG    = $(_USTN_CONFIGURATION)FindSubFolder/FindSubFolder.cfg
_FINDSF_FIND_ROOT = lastdirpiece(DMS_PROJECT(_DGNDIR))
%if exists ($(_FINDSF_CONFIG))
	%include $(_FINDSF_CONFIG)
%endif