How to lock a file in Windows without changing it or using third party tools?

24

11

Is there a way to lock a file with on-board tools so it can't be deleted or overwritten?

For testing of copy scripts I need to lock files temporarily to check the error handling in the scripts. Till XP I used loading a file in debug.exe to lock it.

Is there a way in Windows 7 (and later)?

Edit
I know that there are programs doing this. My question is if there is a built in mechanism in windows. Sometimes I have to check a script on a PC and don't want to install new programs for that.

Edit2
Here are also good suggestions: How to purposefully exclusively lock a file?, which, however, need 3rd party tools or change the file to be locked.

marsh-wiggle

Posted 2014-12-28T13:07:36.747

Reputation: 2 357

Does the notepad trick work for you?

– nixda – 2014-12-28T17:02:26.687

1@nixda No. It overwrites the file. I need to lock a present file without changing it. – marsh-wiggle – 2014-12-28T17:08:44.103

I see. I retract my close vote – nixda – 2014-12-28T17:14:15.733

Answers

45

I think PowerShell is probably the neatest way to accomplish this. Something like the following:

#Specify the file name
$fileName = "C:\myfile.txt"

#Open the file in read only mode, without sharing (I.e., locked as requested)
$file = [System.io.File]::Open($fileName, 'Open', 'Read', 'None')

#Wait in the above (file locked) state until the user presses a key
Write-Host "Press any key to continue ..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

#Close the file (This releases the current handle and unlocks the file)
$file.Close()

While paused, the above script causes the following prompt when attempting to open up "myfile.txt":

enter image description here

Dan

Posted 2014-12-28T13:07:36.747

Reputation: 1 068

2Great! Thanks! To my shame, I must confess that I develop in powershell and did not think about using the .net classes for this. [I hope that will not go any further :-)] – marsh-wiggle – 2014-12-28T14:49:03.090

1

Nice answer. For the curious, here's the MSDN link to the File::Open method detailing the parameters of the call (specifically, the last argument, 'None', declines sharing the file with any other threads/processes - and likewise, provisions exist to lock the file for read/write only, or a combination thereof).

– Breakthrough – 2014-12-28T20:23:33.360

1@moab The file is closed and the handle released at the end. – Dan – 2014-12-28T22:43:24.753

2

Note the $host.UI.RawUI.ReadKey(...) will fail in PowerShell ISE as ReadKey is not implemented for the ISE. The workaround is [void](Read-Host 'Press Enter to continue'). Not exactly the same as the user has to press Enter rather than any key. However, good enough for most purposes. The workaround came from this blog post: http://jeffwouters.nl/index.php/2016/06/powershell-press-any-key-to-continue/

– SimonTewsi – 2016-09-07T04:10:57.840

4

Since I don't like installing software that I won't actually use, I found this jewel of a VBScript:

Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set f = oFSO.OpenTextFile("C:\Tst\My2.txt", ForAppending, True)

MsgBox "Press OK to unlock the file(s)"

All credit goes to Torgeir Bakken, you can see the full post here: http://www.pcreview.co.uk/threads/how-to-lock-files.1518829/

Eriks Ocakovskis

Posted 2014-12-28T13:07:36.747

Reputation: 86

Nice for those who don't like powershell like the solution above – marsh-wiggle – 2016-02-01T14:45:57.897

0

Can you not mark it as read-only? This would effectively "lock" the file by not allowing it to be changed or deleted.

The attrib.exe command is available at the command line, and will allow you set a file or directory to be read-only. More info on that command can be found here

Conversely, you can do the same thing through the GUI by right-clicking the file or directory they you want to mark read-only, selecting properties, and ticking the Read-only box. Clicking Apply/Ok then applies the new setting to the file or directory.

Derek Wirch

Posted 2014-12-28T13:07:36.747

Reputation: 11

3The read-only attribute only prevents from modifications but not from deletions. – ComFreek – 2014-12-28T15:25:58.220

0

Would it not be possible to limit the accessibilty to the file with the Security Tab, when you right-click and go to Properties for the File or Folder?

If the user has the Admin access to the PC, then would this not allow him to remove all others ability to do anything with the file/folder?

I have to admit as I type, that my work PC is still on WinXP Pro, so what I stated above may not be available any longer - I am still not as comfortable with my new Win 8.1 Pro box at home as I am with the years of working with Win XP

Boots66

Posted 2014-12-28T13:07:36.747

Reputation: 1

None of the available NTFS permissions would mimic the behavior of a locked file. The closest would be making the file read-only, but we can be sure the OP is aware of that option and is asking about locking the file for a good reason. – I say Reinstate Monica – 2018-02-19T14:52:43.247

-2

In some scenarios creating new folder with name of the file may do the job. (I.e. get an error trying to open the file).

noonex

Posted 2014-12-28T13:07:36.747

Reputation: 97

This is really a comment and not an answer to the original question. – DavidPostill – 2016-05-25T11:43:37.847

For me it was the answer in task which lead me to this page. Because of people like you somebody will not find it in their similar task. – noonex – 2016-05-26T03:13:53.950

3OP was asking how to lock an existing file. Creating a folder with the same name is not possible. "A subdirectory or file xxx already exists" so your answer is not applicable to the problem in the question. – DavidPostill – 2016-05-26T09:02:42.290

I many cases "lock existing file" is needed to make sure that application will get an error trying to modify or open it. I really will stop explaining obvious things now - have a good day, sir. – noonex – 2016-05-26T11:50:55.153