Results 1 to 15 of 15

Thread: Delete unwanted files [quickest method]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Delete unwanted files [quickest method]

    ok i have a text file which contains names of files....(e.g. joe.jpg,sarah.jpg,etc...) the text file is pretty big with like 24 entries.... the files are found in a folder pic. in this folder, there are also other files.... the folder is like with 120 files... the files are also in subfolders....

    what i want is to delete all files in the folder except the files found in the text file.....

    i have done it like this,
    recursively scan all files in the folder/subfolders

    for each file, compare it with all entries in txt file, it not found delete

    it works well... it is quite quick (3/5 secs max)

    however, the users of my program are claiming that it is taking an infinite amount of time when dealing with a text file with about 150 entries and a folder of 2100 files!!!!

    my question is there a better way to achieve the goal i want here??? ( a quicker way)???

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Delete unwanted files [quickest method]

    Why not just copy the files indicated in the textfile to a temp folder (using relative paths), delete the entire old folder (including subfolders), and rename the temp folder.

  3. #3
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Delete unwanted files [quickest method]

    Nice suggestion leinad.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Delete unwanted files [quickest method]

    I would think copying/pasting all the files into a folder and then deleting it would be even slower.

    Unless you meant moving them which would be a lot faster.

    If that doesn't work out you may want to look into "multi threading".

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Delete unwanted files [quickest method]

    On second thought it would still be slow if there are large files.... (was creating this post and code when DigiRev posted)

    Do it this way instead, dim a new collection. Transfer paths from the textfile to this collection, item being path and key also being path. When that is done recurse your directories, for each path recorsed check if its in the collection (that way you don't have to iterate through the textfile paths). If its in the collection then don't delete.
    Something like below

    VB Code:
    1. Option Explicit
    2.  
    3. Private mCol As Collection
    4.  
    5. Private Sub Form_Load()
    6.    Set mCol = New Collection
    7.    
    8.    mCol.Add "C:\temp\papable.txt", "C:\temp\papable.txt"
    9.    Debug.Print PathInColl("C:\Sample")
    10.    Debug.Print PathInColl("C:\temp\papable.txt")
    11. End Sub
    12.  
    13. Public Function PathInColl(ByVal CheckPath As String) As Boolean
    14.    PathInColl = False
    15. On Error GoTo ErrHandler
    16.    Debug.Print mCol.Item(CheckPath) 'this line tests the key
    17.    PathInColl = True
    18.    Exit Function
    19.  
    20. ErrHandler:
    21.    Err.Clear
    22. End Function
    You may also want to store the folder info from textfile in some data structure... so you can also do per folder tests. By immediately deleting folders, you skip iterating and recursing them.
    Last edited by leinad31; Jan 10th, 2007 at 03:19 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    Quote Originally Posted by leinad31
    Why not just copy the files indicated in the textfile to a temp folder (using relative paths), delete the entire old folder (including subfolders), and rename the temp folder.
    haven't thought about that!!!! nice idea!!! thanks..

    edit:
    saw your code... thanks in my program i have stored the text file entries in a listbox and i use the listbox instead of the text file to check whether file scanned in directory is in listbox... what's the difference with collection is it faster?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    leinad31... i dun understand why it wud be faster with the code you provided... i am still scanning all the files recursively and checking each file with each member of the collection.. isn't it??

    i think you first idea was better... copy the files found in the text file in a temp folder..delete everything in main folder and recopy the files in temp folder to main folder....

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Delete unwanted files [quickest method]

    If there are large files then you will have to wait for their copy to finish which is slow... moving them would be faster. And create the temp folder at the same level of the main folder, so you can just rename it afterwards rather than doing another move of folders.
    Last edited by leinad31; Jan 10th, 2007 at 06:49 AM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    Quote Originally Posted by leinad31
    If there are large files then you will have to wait for their copy to finish which is slow... moving them would be faster. And create the temp folder at the same level of the main folder, so you can just rename it afterwards rather than doing another move of folders.
    oh ok but my files are generally small.... (20-30kb).... how do i move the files using filesystemobject??? is it possible with it?? and how to rename a folder?

    edit: lol there's the methods movefile and movefolder for the fso object..lol silly question above :P
    Last edited by lplover2k; Jan 10th, 2007 at 09:04 AM.

  10. #10
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Delete unwanted files [quickest method]

    Are you using the FSO?
    Better use Dir and Kill.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    i prefer fso... but i know dir and kill are better i am just used to fso

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Delete unwanted files [quickest method]

    Just use Name to move files.

    Name <existing path and filename> As <new path and filename>

    It's an intrinsic VB procedure.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    i get this error when moving folder

    "runtime error 70
    Permission denied"


  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Delete unwanted files [quickest method]

    Which folder is this? The temp folder after deleting main folder?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: Delete unwanted files [quickest method]

    moving the temp folder to location of deleted main folder... i think i found a solution... this usually happens when my prog is in "C:\" e.g. and the main folder is in "D:\" and moving temp folder (found in my prog's directory) i.e. from "c:\" to "D:\" .. so i have changed my prog to create temp in same directory as main folder..delete main folder and rename temp to main folder's name

    thanks again for ur idea!!!! it works much quicker than my previous method

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width