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:
  1. 'first read all existing files into a temp listbox
  2.     Dim strFolder As String
  3.     strFolder = App.path & "\publisher\collectedassets\"
  4.     GetAllFiles strFolder, List1
  5.        
  6.     'we want to erase any file that exists in strFolder
  7.     'but is not listed in the playlist (listview) - because it has become redundant    
  8.  
  9.     asset_in_folder_is_also_in_playlist = False  'begin with supposition that file in folder is not listed in listview
  10.  
  11.     For i = 0 To List1.ListCount - 1
  12.         List1.ListIndex = i 'the file in folder that physically exists
  13.         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
  14.             Set li = lvw.ListItems(j)
  15.             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\")
  16.                 asset_in_folder_is_also_in_playlist = True
  17.             End If
  18.         Next j
  19.        
  20.         'if after having scanned the whole listview, the file does not exist there (but exists in strFolder)
  21.         If asset_in_folder_is_also_in_playlist = False Then
  22.             Kill strFolder & List1.text 'this code SOMETIMES gets executed even when there is no such file in the folder
  23.         End If
  24.    
  25.     Next i

TIA