locking a file

Page 1 of 2 12 LastLast

  1. Posts : 173
    w10
       #1

    locking a file


    I store a data file on my NAS so it can be accessed from multiple computers on my network.
    It is opened with a BAT file that downloads it to the computer, opens the program, then uploads it back to the NAS when the program is closed.
    Is there a way, in the BAT file, to lock the data file on the NAS when it is downloaded, then unlock it when it is uploaded???
    I want to do this so multiple computers can't open the data file at the same time.
      My Computer


  2. Posts : 8,100
    windows 10
       #2

    What sort of file is it some programs you need a special version if the file is used by multiple users like account software.
      My Computer


  3. Posts : 173
    w10
    Thread Starter
       #3

    no, it is a data file-specifically Quicken. I can run it from the NAS, but it runs a lot faster if it is on the computer.
    If I run it from the NAS it is locked when I open QW. Is there some code I can execute from the BAT file to lock it on the NAS, then unlock it?
      My Computer


  4. Posts : 8,100
    windows 10
       #4

    How are files permissions set on the NAS. If it was windows you could set has hidden so no one could see it or set it read only There must be simler on nas. doesnt quicken have a multi user option.

    It would be safer to move the file to pc then nove it back if accounting data gets nackered it can be a nightmare
      My Computer


  5. Posts : 173
    w10
    Thread Starter
       #5

    Samuria said:
    How are files permissions set on the NAS. If it was windows you could set has hidden so no one could see it or set it read only There must be simler on nas. doesnt quicken have a multi user option.

    It would be safer to move the file to pc then nove it back if accounting data gets nackered it can be a nightmare
    Yes, my COMPUTER is a PC. I can set the file as READ only on the NAS using File Explorer. How do I do that in a BAT file?

    - - - Updated - - -

    OK, I figured that one out,
    attrib +r "[file path\name]"
    I don't need to reset it because it is copying back a file that is unlocked.

    now how do I query if the file is read only?
    That would be
    attrib "[file path\name]"

    BUT how do I use an IF stmt to detect the R in the response that will be in the 6th position?
      My Computer


  6. Posts : 295
    Windows 10 Pro
       #6
      My Computer


  7. Posts : 16,914
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #7

    ruggb said:
    how do I use an IF stmt to detect the R
    Here you go.
    Code:
    Set "TargetFile=E:\Wherever\Test & File.pdf"
    If Not Exist "%TargetFile%" ((echo Target file does not exist, error processing needs to be initiated) & (Pause))
    For %%X  in ("%TargetFile%") Do Set "CurrentAttrib=%%~aX"
    If Not "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is set
    If "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is not set
    Pause at end during testing

    Notes:

    1 I included a line acknowledging that errors need to be considered but your script might already provide for the file being absent.

    2 Your script probably already sets a variable for TargetFile so do make sure it makes the same use of quotation marks in the line - Set "TargetFile=E:\Wherever\Test & File.pdf" or just put your target path-filename into my line.

    3 I have tested my code for paths containing spaces and ampersands.

    4 Many people would start with a line @echo off so they would only see the final result [Target file R attrib is ... set] on screen. You can if you want to. I don't. I run my scripts minimised and only look at them if there's a problem in which case I want to see all the processing.

    5 You probably only want one of the lines
    If Not "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is set
    If "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is not set

    and I only included both because I did not know which would fit in best with your existing script. Instead of running an Echo command as I do, you'd want to do whatever it is your script is trying to achieve.


    If you are interested in studying scripting then see my ditty Batch file and PowerShell guides [post #16] - TenForums
    As I state in that post, I think you would get a better return on your investment of time & energy by studying PowerShell rather than studying Windows commands & batch files.



    All the best,
    Denis
    Last edited by Try3; 20 Mar 2023 at 04:01.
      My Computer


  8. Posts : 295
    Windows 10 Pro
       #8

    Way to feed the student instead of making him/her work for their food... LOL!
      My Computer


  9. Posts : 16,914
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #9

    ruggb,

    Thanks for the rep.

    The For command is one of the the most sophisticated & powerful Windows commands and it takes a lot of studying & experimentation.
    It took me a lot of effort to get to grips with it and, since I don't have to use it every day, I rely on the notes I made whilst studying & my sample scripts to refresh my memory when I have to use it again.
    So I thought showing you a solution in my post #7 above would be the best way to get you started. It is both a solution and, hopefully, a useful sample for you.
    - The For command I posted for you includes %%~aX and you'll find that a explained in one of my guide link's demo files - my ditty and demo for: Standard passable variables [post #47] - TenForums - rather than in explanations of the For command itself.
    - The use of %CurrentAttrib:R=% is explained in another of my guide link's demo files - my ditty and demo for: Manipulating variables in batch files [post #14] - TenForums [Look over halfway down for the section headed REM ::::: Testing for particular characters :::::]
    Your desire to understand what I wrote is laudable but please don't forget the comment I made above & in my guide link - I think you would get a better return on your investment of time & energy by studying PowerShell rather than studying Windows commands & batch files.

    Whilst I generally use SS64 to get explanations of Windows commands & PowerShell, in this particular case I think the MS explanation is also very good.
    For - MSLearn


    Best of luck,
    Denis
      My Computer


  10. Posts : 173
    w10
    Thread Starter
       #10

    Try3 said:
    Here you go.
    Code:
    Set "TargetFile=E:\Wherever\Test & File.pdf"
    If Not Exist "%TargetFile%" ((echo Target file does not exist, error processing needs to be initiated) & (Pause))
    For %%X  in ("%TargetFile%") Do Set "CurrentAttrib=%%~aX"
    If Not "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is set
    If "%CurrentAttrib:R=%"=="%CurrentAttrib%" Echo Target file R attrib is not set
    Pause at end during testing
    Denis
    Thank you Try3
    I am not a programmer, just an OLD BSEE. I can follow most programming and create simple BAT files to mildly complex ones with help like yours. It has been a VERY long time since I was proficient with CPM assy code. But some code takes a while to figure out. This is a little more involved and it confuses me, as I only do this on occasion and forget things rapidly [CSR]. HOWEVER, I did get it all figured out and worked all day to get it "perfect". This is the result. (My actual path & filename are replaced with "path/filename".)


    Code:
    @echo off
    :: set color to white on blue
    color 17
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    Set "TargetFile=\\path\filename.QDF"
    For %%X  in ("%TargetFile%") Do Set "CurrentAttrib=%%~aX"
    If Not "%CurrentAttrib:R=%"=="%CurrentAttrib%" (
    :: set color to white on red
    color 4f
    echo Q U I C K E N   I S   O P E N   O N   A N O T H E R   C O M P U T E R
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    echo:
    (pause) & (exit))
    echo:
    echo     ******************************************************************
    echo:
    echo     NOTE: QUICKEN CANNOT BE OPENED ON MORE THAN ONE COMPUTER AT A TIME
    echo:
    echo     ******************************************************************
    echo:
    echo     This BAT file first looks to see if the QDF on the NAS is Read Only.
    echo     If it is, a message is displayed on a red window, and it exits after
    echo     a key is pressed. If it is there, these instructions are displayed
    echo     on a blue window, the NAS file is set to Read Only and the QDF file
    echo 	 is copied to the computer with xcopy and verify.Then it opens QW.
    echo     When QW closes, the QDF file is copied back to the NAS with verify
    echo     using xcopy which resets the RO bit.echo:
    echo:
    echo:
    echo     *******************  DO NOT CLOSE THIS WINDOW  ********************
    echo: 
    echo           IT MUST REMAIN OPEN TO COPY THE QDF FILE BACK TO THE NAS.
    echo:
    echo     *******************************************************************
    echo:
    echo:
    echo     ******************************FILE IS BEING COPIED TO COMPUTER
    echo:
    xcopy /y /v "%TargetFile%" "path\"
    attrib +r %TargetFile%
    "C:\Program Files (x86)\Quicken\qw.exe"
    echo:
    echo:
    echo:
    echo:
    echo     ******************************FILE IS BEING COPIED BACK TO NAS
    echo:
    echo f|xcopy /Y /v "path\filename.QDF" "%TargetFile%"

    Thanks again.
    Bill
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 00:47.
Find Us




Windows 10 Forums