Take the 2-minute tour ×

I've got a large folder on an offsite backup machine that gets populated with files by rsync (through deltacopy) every night (running windows xp) from the main work site. I've discovered some annoying folders that cannot be opened, or deleted, or even checked for file sizes. I get the such and such a folder is not accessible, access is denied message when i try to click on it in windows explorer. According to the windows explorer tooltip they are also "empty" and the properties of these folders say 0 bytes and 0 files.

I currently have a c# program that goes through every folder and file and tries to copy the whole backup directory to a dated backup-backup directory, which is how i discovered this problem in the first place. The regular System.IO library seems helpless against these blasted folders. Exceptions are thrown when I even try to access the folder path.

Does anyone have any clue how I could, say, on an access denied exception in my existing copy code, force the delete of these folders so rysnc can recreate the directory again and get the whole thing synced again?

share|improve this question
Can you attempt to access these folders with elevated permissions? –  Dan Tao Jul 24 '10 at 0:57
not sure what you mean, I'm running as the administrator. How do I get higher permissions than that? –  IBC Jul 24 '10 at 1:03
On Windows XP there is no distinction between administrator and elevated rights. Starting with Vista you can have an administrator account but still have processes running with a restricted token. –  Ben Voigt Jul 24 '10 at 1:13
I do realize you want a programmatic solution, but this isn't so much a C# question as a "how do I get my OS to do this?" question. Therefore, you might want to look into how the various locked file deletion tools work. Here's one product that includes a list of many others, purely for research: ccollomb.free.fr/unlocker –  Steven Sudit Jul 24 '10 at 1:25
@softwareGeek: I wasn't trying to be funny. What was the problem with what I wrote? What struck you as "funny"? –  John Saunders Jul 24 '10 at 1:51
show 16 more comments

4 Answers

up vote 0 down vote accepted

The trouble was that SYSTEM owned these files. I set deltacopy to run as administrator so that administrator would own the files deltacopy makes.

I guess windows is doing its job. The permissions are airtight. But if this happens again someday where I'd have to grab ownership from some other user to the current user (who has administrator permissions) how would I do that in code?

A question for another day I suppose. Thanks again everyone.

share|improve this answer
add comment

It sounds like the filenames are either bad or contain characters that are invalid in Win32. Did you try to delete the directories with rd /r? Did you do a dir /x on them and try to delete the files/directories using their short names?

I would say that you first have to figure out why you can't delete the folders. Once you figure that out, you can write a program to fix it.

OK, so now that you know it's a permissions problem, the first step is to take ownership of the files (so you can set the permissions), then change the permissions so that you can delete the files.

Here's code to take ownership of a file:

WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent(); 

FileSecurity acl = File.GetAccessControl(filename); 

File.SetAccessControl(filename, security); 
share|improve this answer
access denied. access denied. –  IBC Jul 24 '10 at 4:47
What does cacls say? Is it possible the directory is owned by another use and you don't have permissions for it? –  Gabe Jul 24 '10 at 5:26
Okay, I was led on a wild goose chase. I don't know why my thinking was so unclear today. Anyhow, it was a permissions issue. System owned those files. chkdsk led me astray by finding orphaned files in that directory. Grrr. I will look into how to change the permissions on folders in C# if that is possible. –  IBC Jul 24 '10 at 8:30
Changing permissions is easy in .NET, but you have to actually understand ACE's, DACL's, SDDL and the rest. Start here: msdn.microsoft.com/en-us/magazine/cc135979.aspx –  Steven Sudit Jul 24 '10 at 15:32
I ended up making deltacopy server run on the administrator account. Now there aren't anymore permissions problem. Thanks for everyone's help! –  IBC Jul 25 '10 at 21:13
add comment

Yes, try the awesome "Process Explorer" from Microsoft (formerly SysInternals).

Although it's for the processes in the windows filesystem, you could search for your folder in the explorer window & it will tell you who is locking it.

Once you release the process, your program would be able to delete the folder.

If that doesn't work, see if you can specify additional parameters to bruteforce the delete in your program.

share|improve this answer
He wants a programmatic method of dealing with it, therefore, it needs to be done via code automatically, not a manual process using a third party app. –  Dan McGrath Jul 24 '10 at 0:59
Yes, only if he can kill the process that's keeping the folder, will his program be able to delete it. what's wrong with that approach? –  SoftwareGeek Jul 24 '10 at 1:00
Tried this, I searched for the folder in the process explorer search with no results. Besides, assuming this keeps happening once in awhile I need to have a procedure to deal with it automatically, hence the plea for C# help. –  IBC Jul 24 '10 at 1:01
ok understood but my answer doesn't deserver a downvote. –  SoftwareGeek Jul 24 '10 at 1:02
@SoftwareGeek: first of all, my comment should be a hint that your answer would have been better as a comment, since it doesn't answer the question. Also, my comment is different: I say "find out what's wrong, then maybe you can write a program to fix it". I meant, find out once, then the program would be able to fix it many times. –  John Saunders Jul 24 '10 at 2:01
show 12 more comments

First thing I think of when I see this is time to do a checkdisk. From the sounds of it, it feels more like a file system problem than something solvable the way you want to go about it.

share|improve this answer
chkdsk did find some relevant irregularities, in fact. But it did not fix the problem for me. <sigh> I guess the move to linux is unavoidable. –  IBC Jul 24 '10 at 2:10
@IssacB: You can move to Linux if you wish, but do consider the fact that, somehow, people manage to work with Windows and NTFS without being blocked by the problems you're encountering. –  Steven Sudit Jul 24 '10 at 5:26
I think you are right Joel. My solution to this will be to use my copy program to identify broken files and directories and email them to me. Then I'll use unlocker to delete them manually. This probably happened because I'm using a very unreliable POS computer for this job, and it decided to crash in the middle of a write or something. Thanks for recommending unlocker again steven, that is a pretty useful program! –  IBC Jul 24 '10 at 7:34
Sorry, this was not the problem. I have copied the same folder over again through rsync and I have the same problems. I can access the files fine on the main fileserver but not on the backup. The mystery continues. –  IBC Jul 24 '10 at 7:44
add comment

Your Answer


