Sign in with
Sign up | Sign in
Your question

Invalid file hand error received when trying to delete files

Last response: in Windows 7
September 25, 2010 7:53:18 PM

Hi, I'm a software developer and from within an application, I'm trying to delete a directory tree. Sometimes this does not work and when it doesn't work, I finally found out that it's an invalid file handle error. At least that's what Icacls says.

I have tried many different ways to delete the directory structure, but none will work, but oddly enough, if you try to delete the directory tree outside of the application with windows explorer it will work fine. Even with the application up and running.

I've made sure that nothing in the application has a hold on any of the directories or files.

Does ANYONE know a command line to run to reset those attributes for a whole directory tree?

I would appreciate any help!
a b $ Windows 7
September 26, 2010 4:41:41 AM

rmdir from a elevated command prompt
October 3, 2010 10:30:00 PM

thanks man... will give it a try. I used "RM -rf" but that didn't work.
October 6, 2010 12:09:00 AM

Ok... if anyone else runs into this, I was able to come up with something that made things work. Take a look at Icacls and the things it can do. Here are the commands that I ran in my application that might help someone. NOTE: AppPath + "\Output" and inPath are the same thing and they are both talking about a directory.

s.Execute("takeown /f " + AppPath + "\Output")

s.Execute("Icacls " + inPath + " /verify /t /c /l /q")

s.Execute("Icacls " + inPath + " /reset /t /c /l /q")

s.Execute("Icacls " + inPath + " /grant Administrator:( D,WDAC) /inheritance:e|" + inPath)

The first thing I do is take ownership of the directory (I'm an admin)

next, verify: finds all files whose ACL is not in canonical form or whose
lengths are inconsistent with ACE counts.

then, reset: replaces ACLs with default inherited ACLs for all matching files.

and last, grant permission: Will grant the user Administrator Delete and Write DAC permissions to file.

After doing all of the above, I don't have a problem deleting the directory, even if some of the file the attributes are mangled.

So, I hope this helps someone.
Related resources