locking files in DOS

Posted on 2011-04-21
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
    LVL 25

    Accepted Solution

    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 M
    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
    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?

    LVL 43

    Expert Comment

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

    Expert Comment

    by:Ron M's been a looong day.

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

    However... if this is on Windows 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

    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
    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
    Thanks for the points.  What did you use in the end btw?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    #Citrix #POC #XenDesktop #vCenter #VMware #ESX
    I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now