|
-
Jan 10th, 2007, 02:44 AM
#1
Thread Starter
Addicted Member
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)???
-
Jan 10th, 2007, 02:56 AM
#2
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.
-
Jan 10th, 2007, 03:03 AM
#3
Re: Delete unwanted files [quickest method]
-
Jan 10th, 2007, 03:07 AM
#4
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".
-
Jan 10th, 2007, 03:15 AM
#5
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:
Option Explicit
Private mCol As Collection
Private Sub Form_Load()
Set mCol = New Collection
mCol.Add "C:\temp\papable.txt", "C:\temp\papable.txt"
Debug.Print PathInColl("C:\Sample")
Debug.Print PathInColl("C:\temp\papable.txt")
End Sub
Public Function PathInColl(ByVal CheckPath As String) As Boolean
PathInColl = False
On Error GoTo ErrHandler
Debug.Print mCol.Item(CheckPath) 'this line tests the key
PathInColl = True
Exit Function
ErrHandler:
Err.Clear
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.
-
Jan 10th, 2007, 04:13 AM
#6
Thread Starter
Addicted Member
Re: Delete unwanted files [quickest method]
 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?
-
Jan 10th, 2007, 05:50 AM
#7
Thread Starter
Addicted Member
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....
-
Jan 10th, 2007, 06:36 AM
#8
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.
-
Jan 10th, 2007, 07:01 AM
#9
Thread Starter
Addicted Member
Re: Delete unwanted files [quickest method]
 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.
-
Jan 10th, 2007, 09:24 AM
#10
Frenzied Member
Re: Delete unwanted files [quickest method]
Are you using the FSO?
Better use Dir and Kill.
-
Jan 10th, 2007, 09:47 AM
#11
Thread Starter
Addicted Member
Re: Delete unwanted files [quickest method]
i prefer fso... but i know dir and kill are better i am just used to fso
-
Jan 10th, 2007, 11:15 AM
#12
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
-
Jan 10th, 2007, 11:21 AM
#13
Thread Starter
Addicted Member
Re: Delete unwanted files [quickest method]
i get this error when moving folder
"runtime error 70
Permission denied"
-
Jan 10th, 2007, 09:03 PM
#14
Re: Delete unwanted files [quickest method]
Which folder is this? The temp folder after deleting main folder?
-
Jan 12th, 2007, 06:21 AM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|