how to prevent certain files from being deleted using IO.filesystemwatcher

We have a "Master Directory" the contains a subfolder for each customer.  Our account managers are allowed to add change and delete these folders as desired.  About once every few weeks a manager will be using windows explorer when her finger "twitches" at the wrong time and a customer folder accidentally gets moved.   For instance things might look like this.
Master Directory
..AAA Plumbing Cheap
..Cute Scoop Ice Cream
.... Best Nails   ' <=== Best Nails was accidentally dragged to be under Cute Scoop.  
                         Usually, the accident goes unnoticed until several weeks later  !!!!
..Super Newstand

I think the following powershell script will greatly improve things, by asking
"Are you sure you want to move "Best Nails" to be "Cute Scoop\Best Nails (Yes/No)?"
if the manager says "No", the event cmdlet moves the folder back to the original location

Unfortunately,  I can't figure out how to "undo" an accidental deletion. The folder being watched is on a server,  so I cannot simply recover it from the recycle bin.  

Does anybody have any ideas?


# to test this use <  Set-ExecutionPolicy bypass -scope process  > 

Unregister-Event Folderdeleted
Unregister-Event FolderRenamed

$folder = 'c:\aatmp' # Enter the root path you want to monitor. this works windows 7 pro with and evern works if $folder points to an  sbs2003 mappedz mapped to S:.
$fsw = New-Object IO.FileSystemWatcher $folder
$ignoreevent = 0

$vbcrlf = @"


Register-ObjectEvent $fsw Renamed -SourceIdentifier FolderRenamed -Action { 
$name = $Event.SourceEventArgs.fullPath 
$oldname = $event.SourceEventArgs.Oldfullpath 
if ($ignoreevent -eq 1) {
$ignoreevent = 0
} else {
    $a = new-object -comobject
    $intAnswer = $a.popup("Are you sure you want to change Master Directory name from $vbcrlf'$oldname' to $vbcrlf'$name' ", `
    0,"Delete Files",4)
    If ($intAnswer -eq 6) { #you answered Yes
      } else {
      $ignoreevent = 1
      move-Item $name $oldname # <<<<<<<<<<<<<<<<< how to prevent second  'are you sure' message?

Register-ObjectEvent $fsw Deleted -SourceIdentifier FolderDeleted -Action { 
$name = $Event.SourceEventArgs.Name 
$timeStamp = $Event.TimeGenerated 

$a = new-object -comobject
$intAnswer = $a.popup("Are you sure you want to delete Master Directory named  '$name' ", `
0,"Delete Files",4)
If ($intAnswer -eq 6) { #you answered Yes
  } else {
  $a.popup("You answered no. I need more code")
  # how do I prevent deletion

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian ScholerCommented:
There is no way to "undo" a deletion like that. If you have snapshots enabled on the volume, you could pull files from "previous versions", but then you'd have to programmatically enumerate the snapshots so you found the right one, possibly translated that into a UNC path you could access from the machine running the script, then copy the files back out.

But you began by describing someone dragging the folder into another folder, which is not a deletion. So this workflow will result in multiple copies of the files, some possibly with different contents.

I think the correct way to handle this is to use advanced ACLs on what you term the "master" folders to prevent the users from deleting them. You can not allow them modify on the master (using This Folder Only), or perhaps simpler is you could actually use Deny only with the Delete privilege, again applied to "This Folder Only". It won't prevent them from making a new copy, but it won't remove the old.

Another solution I've seen, if the problem is just clumsy drag and drop, is set the DragHeight and DragWidth in the registry under HKEY_CURRENT_USER\Control Panel\Desktop. You can set it to a value that corresponds to a number of pixels, so if they're set to 50, then it has to be dragged for 50 pixels for the drop to be successful. Kind of janky, but it can help.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rberkeConsultantAuthor Commented:
I described two problems. I already posted the solution to problem #1 which is an accidental move from a twitchy finger.

Problem #2 (which happens much less often) is when a user accidentally selects the wrong folder then deletes it.  

You said there is no way Filesystemwatcher can warn the user BEFORE they are about to delete the wrong folder.   This confirms what I had previously thought, so I am closing this question.  

But I will program Filesystemwatcher to send a message AFTERWARDS saying "you have deleted folder <XYZ>, if this was a mistake, please contact rberke and have him restore the deleted folder".


P.S. We previously tried using security to assign some users the ability to change folders and others only to read, but it didn't work out.  Its a small shop and users are often sharing folders.
rberkeConsultantAuthor Commented:
Your "janky" solution is kind of interesting.  I prefer my filewatcher approach, but I will keep your idea in mind for the future.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.