问题
I am trying to write a DigitalMicrograph script which opens all images containing a specific string in the file name.
I know how I can open an image using OpenImage( filename )
and I have seen in the documentation that a command GetFilesInDirectory()
exists, which seems to be what I need. However, I do not understand how I can use this command. Can somebody give me a code snippet demonstrating this, please?
回答1:
The command GetFilesInDirectory()
gives you a TagList of all files / subfolders in a given directory. This is shown in the following example:
String folder
TagGroup FileList
number fFiles = 1
number fFolders = 2
If ( !GetDirectoryDialog( "Select base folder", "", folder ) )
Exit(0)
FileList = GetFilesInDirectory( folder, fFiles + fFolders )
If ( FileList.TagGroupCountTags() > 0 )
FileList.TagGroupOpenBrowserWindow( "Files & Folders", 0 )
This script will show you the resulting TagGroup in a browser window like the one below. Each list entry is itself a TagGroup which contains a single tag "Name". This tag contains the file or folder name. You can use the command to either give you only files, only subfolders, or both.
Once you have the TagGroup of all entries, you process is like any other TagGroup in DigitalMicrograph. For example, you can browse the list to read out the strings and simple print them to the results window like this:
number nTags = FileList.TagGroupCountTags()
for ( number I = 0; I < nTags; i++ )
{
TagGroup entryTG
FileList.TagGroupGetIndexedTagAsTagGroup( i, entryTG )
if ( entryTG.TagGroupIsValid() )
{
string filestr
if ( entryTG.TagGroupGetTagAsString( "Name", filestr ) )
{
Result( "\n File:" + filestr )
}
}
}
来源:https://stackoverflow.com/questions/26713158/opening-multiple-files-from-folder-in-digitalmicrograph-scripting