program FolderTree ; { Batch process all files in a folder and all its sub-folders } { Digital Optics, Nov 2000 } { Additional error check on folder name added Aug 2001 } { This example illustrates how to traverse an entire directory tree looking for files and processing them individually. Note that files will be found in the order they are stored and not necessarily in alphabetical order. } { To add your own code see the ProcessFile() procedure below. } { NOTE: This code assumes that folder names have no file extensions } button btn_FolderCamera,'Search folder tree' ; const { settings } SearchSub = true ; // true if you want to seatch all sub folders Progress = true ; // true if you want to report progress on the status bar FirstSub = 3 ; // do not change var { global variables } BaseFolder ; Wildcard ; FileList ; nSub ; procedure ProcessFile( Name ) ; { This procedure is called once for every file in the folder tree. To implement your own file processing module just add code to this procedure. The default procedure simply writes each file name to the master list and optionally onto the status bar for user feedback. } begin { record the file name } writeln( FileList,Name ) ; if Progress then WriteStatus( Name ) ; { custom processing goes here } end; { ProcessFile } procedure ListSubFolders( Top,List ) ; { Create a list of sub folders } var Folder ; begin Folder := FindFirstFile( Top+'\*.*',fa_Directory ) ; while Length( Folder ) > 0 do begin if ( Length( Folder ) > 0 ) and ( Length( ExtractFileExt( Folder ) ) = 0 ) then begin writeln( List,Folder ) ; end; Folder := FindNextFile ; end; end; { ListSubFolders } procedure SearchFolderTree( Folder ) ; { Recursively search a folder tree for files } var SubList ; FileName ; i ; begin if Length( Trim( Folder ) ) > 0 then begin { process files in current folder } FileName := FindFirstFile( Folder+'\'+Wildcard,fa_Archive ) ; while Length( FileName ) > 0 do begin ProcessFile( FileName ) ; FileName := FindNextFile ; end; { look for sub folders } if SearchSub then begin { generate a temporary sub-folder list } nSub := nSub + 1 ; SubList := CreateEditor( Str( 'SubList',nSub ) ) ; Minimize( SubList ) ; ListSubFolders( Folder,SubList ) ; { search sub-folders } for i := FirstSub to GetLineCount( SubList ) do SearchFolderTree( ReadEditor( SubList,i ) ) ; { delete sub-folder list } Delete( SubList ) ; end; end; end; { SearchFolderTree } begin { init } BaseFolder := 'C:\Documents' ; Wildcard := '*.*' ; { ask for base folder } if GetString( 'Enter the folder to search:',BaseFolder ) = id_Ok then begin { ask for file template } GetString( 'Enter the file type description (eg. *.tif):',Wildcard ) ; { create main file list } FileList := CreateEditor( 'File List' ) ; Minimize( FileList ) ; nSub := 0 ; { search the folder tree } WriteStatus( 'Processing folders...' ) ; SearchFolderTree( BaseFolder ) ; WriteStatus ; { show file list } Restore( FileList ) ; end; end.