Solved

ASP FSO delete files from server that are not in database

Posted on 2011-02-13
6
679 Views
Last Modified: 2012-05-11
Hello All,

I am working on a classic ASP project and trying to loop through folders and delete certain files.
I have a main folder which has many subfolders.
Each subfolder can be empty or can contain multiple files.
I have the list of “in-use” files in a database. These are files I do not want to delete. I want to delete all files off the server that do not exist in the database.
The same filename can exist in different folders, so filename itself is not unique, however the folder + filename is unique.

I’ve worked on this for over a day and still cannot get it right; here’s what I have (not working code):


<%
Dim dbFilePath, DaFolder, objFSO, mainFolder, aFolder, aFolderCollection

dbFilePath = rsGetFiles.Fields.Item("varPath").Value
'varPath field = foldername and file name (17\sample_upload_me.pdf)
' Also have these fields available:
'varFileName = sample_upload_me.pdf
'varFolderName =17

DaFolder = Server.MapPath("../Certification/Tracking") & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set mainFolder = objFSO.GetFolder(DaFolder)
Set aFolderCollection = mainFolder.subfolders

Do Until rsGetFiles.EOF


For Each aFolder in aFolderCollection

      For Each aFile in aFolder.Files
      
            sFilePath = aFolder.name & "\" & aFile.name
             
                  If sFilePath <> dbFilePath AND objFSO.FileExists(aFile) Then
                        'If objFSO.FileExists(aFile) Then
                              'objFSO.DeleteFile(sFilePath)
                        'End If
                  End If
            
      Next 'file
Next 'folder

rsGetFiles.MoveNext
loop       
%>


Thanks in advance for your help!

-g8tor
0
Comment
Question by:Jim M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 34882866
Rather than using the list of valid folders/files as the outer loop, I would just traverse the folders/subfolders and hit the DB to verify keep or discard.

You are working with a negative list, that is, you want to delete everything not in the DB. You must have a boundary -  a starting and ending folder - but you will still have to check every file within that boundary against the DB.

OR...

Run through the DB and look for a match on the hard drive. If the folder/file is found, move (or copy) it to a new location (c:/TempNew/folder/filename). When you have checked for all valid files in the DB, delete the old folders/files and move the new location to the old location.

This is more efficient (and simpler) because you know what to look for.
0
 
LVL 2

Author Comment

by:Jim M
ID: 34882882
Thanks for the post...

"Rather than using the list of valid folders/files as the outer loop, I would just traverse the folders/subfolders and hit the DB to verify keep or discard."

Yes, that is what I am trying to do, I just do not know how to structure it. Can you post working code?
0
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 34882905
Not without some sort of incentive.

Basically, I would:

1. Get the next folder and file from the DB
2. Use FSO to see if it exists
3. If it does, copy it to the new location
4. If there are more entries in the DB, return to step 1
5. Delete all of the old folders/files
6. Move the nw folders/files to the old location
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Jim M
ID: 34883328
Ok, I found a solution, though it is not the ideal solution I was hoping to get. I adjusted the code on some of the other pages to delete the old file before the associated record is deleted, and delete the old file before a new one is uploaded for the same record. Then once a day I'll just delete all folders (and the files within them) where the person associated with them is no longer a member. That query yields a "positive list" and I am essentially using your step #2.

0
 
LVL 2

Author Closing Comment

by:Jim M
ID: 34883334
Was hoping for working code, but apparently 500 points wasn't sufficient 'incentive'. Solution gave me ideas about how to reconfigure my approach to the problem, but was only a partial solution.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34884385
You can always show your work and ask for help.

What you may not get is someone else's working code.

It's always nice to talk to you before you go. If you would have shown me what you came up with, I would gladly have commented on that.

I was not in any way being antagonistic - this isn't a "you" vs "us" kind of place; we are in this together. I'm sorry if your ecpectations weren't met.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question