[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


locking files in DOS

Posted on 2011-04-21
Medium Priority
Last Modified: 2012-06-27
is it possible to do file locking using DOS commands?  namely, i am referring to a reader/writer lock.  or does it have to be using a high level language such as c, c++, c#?
Question by:mmingfeilam
  • 4
  • 3
LVL 25

Accepted Solution

Ron Malmstead earned 1000 total points
ID: 35444470
For what purpose ?

What is your actual problem ?, there maybe alternate solutions if you supply more details.

File type ?

Locking occurs when reading. or writing.

If it was a text file...
You could read the file in DOS which should lock it, using Find or Findstr . C:\filename.txt
..and looping until a condition is met.
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35444477
Permissions change would also lock it, if the process you are locking it from is running under an account that doesn't have permissions.  So you could also use Attrib to lock it for a specific account, then change permissions back to unlock.
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1000 total points
ID: 35444486
You can't lock the file as in place a file or record lock on it from command prompt / batch since that requires the file to be open and "in use".  You can make the file read only, using the ATTRIB command or amend the NTFS permissions using the CACLS command.


attrib +r somefile.txt
(or *.txt, or add /s for subdirs too)

cacls is obviously more complicated as you are revoking or granting a specific user/group access to a file and is therefore related to what rights are already there and inherited etc. from parent directory too.

From VBScript etc. or even ancient DOS based QuickBASIC etc. you can place file and record locks on files/databases.

What are you wanting to do?

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 43

Expert Comment

by:Steve Knight
ID: 35444494
Was clearly a little slow typing all that lot, please clarify your purpose and can advise more.
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35444528
...errr...it's been a looong day.

I said attrib, but I actually meant Calcs as dragon-it stated.

However... if this is on Windows 7...you could make the file "hidden"... and if the program is running under an account that is not creator/owner, it won't be able to "see" it.

Author Comment

ID: 35444636
our CTO wants to accomplish the following using a batch file (the batch file is run by a file trigger agent, e.g. when there is a file in the folder, then the batch file will run).  The aim is to encrypt and then transfer files in a specific sequence.


if (LockFileIsPresent() {

Process any files that we were in the middle of transferring when we aborted

//Process files in prefix order
while (files exist with prefix) {

      // renaming file

      // Transfer the file
      rc = SFTP file;

      if (rc != SUCCESS) {
            return rc;

return SUCCESS;

i am not sure if it's possible, or even the best solution when languages such as java and c# already have locking mechanisms built-in.  but before i question his approach, i want to find out more about how feasible his solution is.
LVL 43

Expert Comment

by:Steve Knight
ID: 35444749
OK, forget locking then.  Best bet is to rename or move the files, i.e.

move all files to subidr "processing"
@echo off
set maindir=c:\sourcedir
set processing=%maindir%\processing
cd /d "%maindir%"
mkdir "%processing%" 2>NUL
move *.* "%processing%"

at that point only any files that weren't locked and unable to move will be moved to the processing directory.  Work on the processing directory with the next bit of your script processing all files in that dir with your encryption command.  If it needs them individually then use a for loop to read the filenames or pass the encryption command "%processing%\*.*" as the path.

Have I understood correctly there?

LVL 43

Expert Comment

by:Steve Knight
ID: 35471571
Thanks for the points.  What did you use in the end btw?


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question