I have got a listview where users can make a list of files. Now when they press OK I want to scan the folder to see if a file in the folder is also in the listview. If not, that file is to be deleted - so that the folder contains only those files that are in the list. The following is the code I am using and I think there is much better and neater way of doing it. Also sometimes the code to delete file gets executed even if there is no such file.
Any advice to do this better way ?
VB Code:
'first read all existing files into a temp listbox Dim strFolder As String strFolder = App.path & "\publisher\collectedassets\" GetAllFiles strFolder, List1 'we want to erase any file that exists in strFolder 'but is not listed in the playlist (listview) - because it has become redundant asset_in_folder_is_also_in_playlist = False 'begin with supposition that file in folder is not listed in listview For i = 0 To List1.ListCount - 1 List1.ListIndex = i 'the file in folder that physically exists For j = 1 To lvw.ListItems.Count 'for each file in playlist, check whether the file in listbox (ie physically exists) is listed in playlist Set li = lvw.ListItems(j) If li.text = List1.text Then 'list1.text has only filename not path (the file is contained in strfolder i.e. App.Path & "\publisher\collectedassets\") asset_in_folder_is_also_in_playlist = True End If Next j 'if after having scanned the whole listview, the file does not exist there (but exists in strFolder) If asset_in_folder_is_also_in_playlist = False Then Kill strFolder & List1.text 'this code SOMETIMES gets executed even when there is no such file in the folder End If Next i
TIA




Reply With Quote