FUNCTION DeleteEmptyFolders ( TargetPath ) ' released 2021-01-02 ' delete ALL empty folders, including nested ones, under a user defined starting point. ' example of useage: ' DeleteEmptyFolders ( "c:\users\public\\") ' we use database functionality, if a DB is not open then open one ' we will tidy-up at the end dim closeDBatEnd = 0 IF ( ! dbOpen ) then dbopen ("memory") closeDBatEnd = 1 END IF ' we want to give the command dbFileList a proper path ' that means it is terminated by a "\" IF ( right (TargetPath, 1) <> "\\" ) then TargetPath = TargetPath & "\\" END If ' now create a table of ALL of the file system components under the TargetPath dbFileList ( "temp", "myFileSystemList" , TargetPath, -1 ) ' use the FileSystemObject DeleteFolder method to do the actual deleting dim fso = createobject ("Scripting.FileSystemObject") report = "--------------------------------------------" ' if we want to remove "nested" empty folders we need a DO LOOP select count (*) from ( select ( path || name || '\' ) as path from temp.myFileSystemList where type = 'folder' ) where not path in ( select path from temp.myFileSystemList ) DO while ( qrSingleValue ) ' find the empty folders > select * from ( select ( path || name || '\' ) as path , rowid as rid from temp.myFileSystemList where type = 'folder' ) where not path in ( select path from temp.myFileSystemList ) WITHQUERY ( freestring ) report = wqtext(1) ' delete the empty folder runon fso.deleteFolder ( right ( wqtext(1) , -1 ) ) ' remove the empty folder from the DB table, so that we can expose the next level of nested empty folders > delete from temp.myFileSystemList where rowid = freestring = freestring & wqint(2) sql = freestring END withQuery ' now see if we have exposed a nested empty folder select count (*) from ( select ( path || name || '\' ) as path from temp.myFileSystemList where type = 'folder' ) where not path in ( select path from temp.myFIleSystemList ) LOOP ' tidy up the database IF ( closeDBatEnd ) then dbclose () ELSE drop table if exists temp.myFileSystemList vacuum END IF END Function