ASP FSO delete files from server that are not in database
Posted on 2011-02-13
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
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
Thanks in advance for your help!