Scripting Resources for DigitalMicrograph™


Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio | Private | Search


Example: Reading the Contents of a Directory into a Taggroup
An example script which shows how to read the contents of a directory into a taggroup and subsequently use that taggroup to access files in the folder.
20140302, v1.0
D. R. G. Mitchell
Taggroups are convenient data structures for storing information within DM. Run the script and select one file within a folder. The filenames in the folder are read into a taggroup which is subsequently used to access the files. If DM images are contained within the folder they are opened.


System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Included Files
Main script file.
Source Code

// Example code which shows how to read all the files in a directory

// into a taggroup. This is a useful way of storing information on the files.

// Once the information has been read in, it can be accessed by the script to open images.


// In this example, all images in a directory are displayed and the taggroup browser

// opened to show the tag structure


// D. R. G. Mitchell, (remove the nospam to make this work)

// version:20140302, v1.0, March 2014,



// Prompt to select one file in a directory


string path

if(!opendialog(path)) exit(0)



// The directory is extracted from the path


string directory=pathextractdirectory(path,0)



// The contents of the folder are saved into a taggroup


TagGroup dircontents = GetFilesInDirectory( directory, 1)



// The number of tags (files in the folder) are counted


number entries=dircontents.taggroupcounttags()


result("\n\nDisplaying the contents of folder : "+directory+"\n")



// Loop to navigate through the tags opening the files


number i

string thispath


for(i=0; i<entries; i++)


// get the nth tag which holds the image name

string filename

image temp

dircontents.taggroupgettagasstring("["+i+"]:Name", filename)


// create the path to the image and open and display it - but only if it has

// an appropriate extension - ie it is a dm image or a tiff

thispath=pathconcatenate(directory, filename)

string extension=pathextractextension(filename,0)

// Check to make sure the image is an image and not some other file

if(extension=="dm4" || extension=="dm3" || extension=="tif")



result("\nImage "+(i+1)+" : "+filename+" opened.")


else result("\nImage "+(i+1)+" : "+filename+" skipped.")

if(temp.imageisvalid()) showimage(temp)




// display the contents of the taggroup in a taggroup browser window