Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to hold an exclusive lock on a file so it cannot be read or written by anything else. Is there a simple Windows tool or command to do this?

I suppose the tool or utility would implement the LockFileEx Windows Function.

Note: I've tried text editors like Notepad and Notepad++ on a text file but they don't hold an exclusive lock on it.

share|improve this question
    
Why not just make one? Also, how long should they hold the lock? Should they wait for some event? –  Mehrdad Jun 9 '11 at 2:38
1  
I considered programming one but figured there's a quick tool, editor or command I can use. On superuser not all users are programmers. bit.ly/lXT6ey I didn't want to go the Stackoverflow route with this question. Am testing the behaviour of an app when it can't access files. –  John K Jun 9 '11 at 2:53
    
I might be able to make one for you -- just lemme know when the lock should be released. –  Mehrdad Jun 9 '11 at 2:57
    
see also my similar question superuser.com/questions/519389/flock-command-for-windows –  eadmaster Dec 18 '12 at 2:50
    
Apparently, editors such as Notepad and Notepad++ don't even keep the file open non-exclusively. –  John Aug 27 '14 at 15:31

5 Answers 5

up vote 22 down vote accepted

Try Easy File Locker (freeware).

enter image description here

share|improve this answer
    
Does not work on Windows 2003 –  Eric Bonnot Oct 29 '14 at 14:38
    
This application doesn't appear to "Lock" files so much as change the permission on the file. This is an important distinction if you're trying to test how your application handles files being actively opened/locked by another application. In my case this application did not work. The 'notepad > file' solution should be the answer here. –  javajavajavajavajava Jul 20 at 14:48

Simpler solution: In the directory of interest run from cmd line:

notepad >filetolock

As re-directed stdout, it will remain locked until the application (notepad) is terminated.

Note that the "filetolock" will be overwritten by the re-direct, so you probably don't want to use an existing file of any importance. An empty "filetolock" won't matter to the application you're trying to test, as the app won't be able to open it anyway.

share|improve this answer
7  
use append redirect instead to now overwrite filetolock: notepad >>filetolock –  eadmaster Apr 18 '14 at 1:38
1  
very useful solution, no need to install anything –  Aprillion May 1 '14 at 16:06
1  
This is a much better solution than downloading more freeware. –  BenCr Oct 6 '14 at 12:30
1  
Great sulution. The locked file can't be deleted, but it can be copied. –  boboes Jan 5 at 13:23

Open it with MS-Excel... this app locks a file while open.

share|improve this answer
4  
And I thought Excel was useful before! –  Scott Stafford Oct 4 '12 at 19:58

Lock a file without 3rd party tools, no changes to the file beeing locked and file can't even be copied

This powershell script is a quote from an answer to a similar question.

If you find it helpfull, you may upvote the original answer and not this posting.

#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
$file.Close()
share|improve this answer

FileLocker is a freeware/open source command line tool.

Usage:

FileLocker [/T LockTime] [/I] [/K] [/Q] file [file...]

/T LockTime     Time in milliseconds to lock the file
/I              Infinite locking until process is killed (default)
/K              Lock file until key is pressed
/Q              Be quiet.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.