3

Is it possible to configure Windows to automatically close a shared-file read lock when a user with full permissions deletes it via the share? I know that there are ways to do this manually but I want it to happen automatically.

To add more details, I have a host machine running Windows Server 2012 R2. The host is running a Windows 10 VM in Hyper-V. The host is also sharing a folder with permissions such that "Everyone" has read-execute access, and one user on the VM has full access. When I connect to the share from a second machine (as an "Everyone" user), I can access the files (read-only) as expected, open them in the shell, notepad, etc. But if, while I have files open, I attempt to delete them from the share using the VM user, the system hangs as there is a read-lock on the file. If, however, I delete them from the host server, the read locks are usually (but not always) forcibly removed. Is there a way to get this behavior consistently when deleting them on the host or through the VM user, e.g. a "write-access-overrides-read-locks" share flag?

Edit:

What I really want is to be able to do is, from both the host server and VM, via both the shell and command prompt, rename and delete files and have the operation always succeed, even if other users have the files open for read via the share.

Edit2:

To clarify what I mean by automatic, I need the read-locks to be ignored even when using the standard commands (e.g. rmdir, erase, etc.) as well as the shell interface, and ideally even Win32 APIs (though the latter is not required). Using a script or other custom command, even if it uses the exact same syntax, won't work unless there's a way to hook the standard commands to use the custom ones. The reason is because I am running third-party batch files and executables that modify the shared files, and those scripts and programs fail out if they're not able to modify the target files. Modifying the third-party files to use custom lock-breaking commands is not an option.

I know that as an alternative to all of this I could run the third-party programs using an internal staging directory, then move over pieces of their output as they are finalized, but my goal is to have the output be made available on the share progressively, not just once each piece is finished.

1 Answer 1

3
+25

We can get pretty darn close with PsExec and a little batch work.

On your server/VM, we'll want to tie in, not complicate, a release command into the existing rename and delete commands in command prompt. The implied priority is for it to be as seamless as possible.

First, to enable PsExec network execution on the clients, make a reg file:

clients-enable-psexec.reg:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"LocalAccountTokenFilterPolicy"=dword:00000001

This will ensure the clients will run commands sent by the server/VM.

On the server side, download PsExec from the TechNet website onto your server VM.

Then, place these .bat files in a system environment directory, so they can be called from anywhere on the server/VM:

x-del.bat (argument: file; substitute for del command)

psexec \\* c:\client-release.bat %~n1   :: \\* = all network clients, %~n1 = filename
timeout /t 3
del %1

x-ren.bat (argument: file, name)

psexec \\* c:\client-release.bat %~n1
timeout /t 3
ren %1 %2

Place this .bat file on all the clients:

client-release.bat (argument: file share to close)

setlocal enabledelayedexpansion
net file > c:\temp.txt
for /F "tokens=*" %%A in (c:\temp.txt) do ( :: Loop through net file output line by line
 echo.%%A | findstr /I /C:"%1">nul && ( :: Check if entry contains file in question
  set vara=%%A
  set varb=!vara:~0,1!  :: First char of line output is PID of of file
  net file !varb! /close
 ) || (
  rem not found, move on
 )
)
del c:\temp.txt

Command Usage:

c:\> del Z:\ServerFolders\file.doc
File could not be deleted: in use
c:\> ren Z:\ServerFolders\file.doc newfile.doc
Access Denied
c:\> x-del Z:\ServerFolders\file.doc
File Deleted Sucessfully
c:\> x-ren Z:\ServerFolders\file.doc newfile.doc
Renamed
c:\>

The Real Override:

If you wanted to override the actual del command, and other internal commands for use in batch files with version control etc, you can use this magic:

setdos /i-del
alias del=c:\windows\system32\x-del.bat
setdos /i-ren
alias ren=c:\windows\system32\x-ren.bat

The catch?

You need a 3rd party command prompt installed alongside Windows default cmd: JPSoft Take Command

Above commands taken from here: Override TCC Internal Commands - JPSoft

3
  • I appreciate the detail you've put into this but unless there is a way to hook up the behavior of x-del and x-ren commands to the standard commands, this won't work for me. This is because the files will be renamed and deleted by other batch files under version control and can't be modified. I'll add these details to the question.
    – MooseBoys
    Commented Jan 22, 2016 at 19:15
  • Hm, I couldn't find anything for rerouting "internal" dos commands on Google, but if I'm super lucky I could load cmd.exe in Resource Hacker tonight and find some promising info in the string tables. Will get back to you. Commented Jan 22, 2016 at 23:49
  • I added more info for overriding internal commands! Check updated answer. Commented Jan 25, 2016 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .