33

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
  • 3,134

5 Answers5

61

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

Nobody
  • 1,268
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/

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.

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

-3

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
  • 105