locking files in DOS

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#?
mmingfeilamAsked:
Who is Participating?
 
Ron MalmsteadInformation Services ManagerCommented:
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.
0
 
Ron MalmsteadInformation Services ManagerCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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.

e.g.

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?

Steve
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Steve KnightIT ConsultancyCommented:
Was clearly a little slow typing all that lot, please clarify your purpose and can advise more.
0
 
Ron MalmsteadInformation Services ManagerCommented:
...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.
0
 
mmingfeilamAuthor Commented:
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.

pseudocode:

if (LockFileIsPresent() {
      return WAITING_FOR_WRITING_TO_COMPLETE;
}

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

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

      // renaming file
      RenameAndRemovePrefixforSFTP();

      // Transfer the file
      rc = SFTP file;

      if (rc != SUCCESS) {
            return rc;
      }

      DeleteFile();
}
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.
0
 
Steve KnightIT ConsultancyCommented:
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?

Steve
0
 
Steve KnightIT ConsultancyCommented:
Thanks for the points.  What did you use in the end btw?

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.