Jump to content

Delete a file or folder with a degree of safety


Recommended Posts

Been a while since I contributed some code in this section of the forum ... mostly because I'm still using older versions of AutoIt.

However, I recently started using the following function, as I thought it was a good idea. I'm sharing it for the same reasons, especially in light of the fact, that people do make mistakes, and sometimes incredibly stupid ones. That's the ever present nature of humanity I guess.

This is only a pretty basic function, taken straight from my latest program, which others may need to modify, as it is mostly centered on folders.

If I was really serious about this, and qualified to make a UDF, then I would add a couple more arguments.

[1] second would be provision of a list of other files/folders to protect. Maybe as a text file, with one path per line.

[2] third would be an exception to an item currently protected, thus giving that power at need.

$res = UnsafeToDeleteChecklist($destfld)
If $res = 1 Then DirRemove($destfld, 1)

Func UnsafeToDeleteChecklist($pth)
    ; Function prevents deletion of file or folder based on path, if an important system one.
    If $pth = @HomeDrive Or $pth = "C:\" Or StringInStr($pth, @WindowsDir) > 0 Or $pth = @ProgramFilesDir _
        Or $pth = @AppDataCommonDir Or $pth = @DesktopCommonDir Or $pth = @DocumentsCommonDir _
        Or $pth = @FavoritesCommonDir Or $pth = @ProgramsCommonDir Or $pth = @StartMenuCommonDir _
        Or $pth = @StartupCommonDir Or $pth = @AppDataDir Or $pth = @CommonFilesDir Or $pth = @DesktopDir _
        Or $pth = @FavoritesDir Or $pth = @MyDocumentsDir Or $pth = @ProgramsDir Or $pth = @StartMenuDir _
        Or $pth = @StartupDir Or $pth = @UserProfileDir Or (StringLen($pth) = 3 And StringMid($pth, 2, 1) = ":") Then
        Return 0
        Return 1
EndFunc ;=> UnsafeToDeleteChecklist

Supposedly, if I remember rightly, Windows protect some files and locations, and has done so since about Win XP, but people change settings, and have more expansive requirements, hence my function. Plus, I'm not willing to test how protected Windows makes things.

Be interesting to know, whether others have thought the same or done the same or have some good stories to tell about accidental deletions, especially with major consequences.

We probably shouldn't discuss that here though, so I will now start a Chat topic, and link back to this topic.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.


What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...