Hidden file or reserved name

It might occur that you cannot delete files on a NTFS volume even if you are member in the Administrators group.

This can have various reasons. Some of them are described in http://support.microsoft.com/kb/320081/en-us.

Cause 5: The file name includes a reserved name in the Win32 name space

This I’ve encountered multiple times now, but it can be fixed using

del \\?\C:\path_to_file\file.dat

to delete that particular file anyway.

The filename or extension is too long.

This happens in very deep folder structures and you can’t delete files or folder afterwards. This can be fixed using a little trick I’ve learned. The goal is to reduce the length of the path to that point where it can be handled by Windows. The command for this use case is subst.

C:\>subst /?
Associates a path with a drive letter.

SUBST [drive1: [drive2:]path]
SUBST drive1: /D

  drive1:        Specifies a virtual drive to which you want to assign a path.
  [drive2:]path  Specifies a physical drive and path you want to assign to
                 a virtual drive.
  /D             Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives.

One example:

C:\>subst G: C:\Documents and Settings\user\Desktop\One Folder\A Subfolder\A Subsubfolder\

Now the content of “C:\Documents and Settings\user\Desktop\One Folder\A Subfolder\A Subsubfolder" is now accessible as drive G:, which now reduces the length of the path and the deletion of a file is not a problem anymore.

Delete folders which I don’t have the permission to do so (Vista/Windows 7)

Start an administrator console (execute cmd.exe with administrator privileges) and go to the folder of interest. Execute the command below to change permissions and grant access to your user.

C:\>icacls "<path to folder or file>" /grant <username>:(D,WDAC) /t

The command will give the <username> user write and delete privileges for that file/folder and all subfolders and files.

As an example:

C:\>icacls "C:\FolderIwantAccessTo" /grant metawave:(D,WDAC) /t