Trying to SetAttr of files needing "Admin" access causes program to hang. No error. Help.

  • Thread starter Thread starter Mugsy_in_Houston
  • Start date Start date
M

Mugsy_in_Houston

Guest
Nearly a decade ago, I wrote a little utility to automatically delete all files from a folder of my choosing that are older then a certain number of days (also of my choosing.) It's mostly for automatically deleting old TV recordings so the folder doesn't fill up forever, but I love using it for temporary storage. I've updated it over time but it has worked like a charm for years.

Today, I finally got around to trying to add the ability to delete folders too... something that has eluded me for years. So I updated my code to first reset any folder attributes back to normal, after which it is supposed to delete the folder and it's contents.

Unfortunately, during testing on a folder containing "protected" files (despite Windows showing all attributes normal), when I try to reset the attributes, the program simply hangs like it is stuck in an endless loop. No error, and no files/folders are changed.

Dim di As New DirectoryInfo(strCachePath) ' Path read from .ini file.
Dim diArr As DirectoryInfo() = di.GetDirectories()
Dim dri As DirectoryInfo
For Each dri In diArr
strFilename = dri.Name
If InStr(LCase(strFilename), "safe-") <> 1 Then ' Ignore folders tagged as "safe".
dteGetDate = dri.CreationTime.Date
dteToday = Format(Today, "d") ' Strip time from date.
dteDays = Format(dteGetDate, "d") ' Ditto
intDateDiff = DateDiff(DateInterval.Day, dteDays, dteToday)
If intDateDiff > intDays Then
strCachePath = strCachePath + "\" + strFilename ' Target folder with path.
' ***** PROGRAM HANGS on next line! *****
FileSystem.SetAttr(strCachePath, vbNormal) ' Clear any Read-Only or "Hidden" attributes.
FileIO.FileSystem.DeleteDirectory(strCachePath, FileIO.DeleteDirectoryOption.DeleteAllContents, FileIO.RecycleOption.DeletePermanently)
End If
End If
Next dri

When I test, the folder in question is: "D:\Recorded TV\TempRec\TempSBE\" containing two 500MB files (all attributes already cleared):

"{2426ECEF-CBF4-49F0-9486-ED1027B120A8}.tmp.sbf"
"{FE3C930D-A7CD-4FBB-ABCB-98C0890A4443}.tmp.sbf"

If I try manually deleting the files by hand, I'm prompted for Admin access.

If I skip resetting the attributes, the program simply hangs on the next line (trying to delete the folder & contents.)

I've tried waiting several minutes for the program to resume but never does. I could run the program as Admin, but it's meant to run automatically at Startup, and being prompted for Admin access at startup is really bad form (annoying and potentially dangerous.)

Is there any way around this? Even detecting such protected files/folders so they can be ignored would help (but I'd much rather delete them.) TIA

Continue reading...
 

Similar threads

Back
Top