CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    How to tell a file is locked wityout opening (very fast)

    Hi,

    I need to find if a file is locked, but I dont want to open the file as I will be checking 1000's of files.

    can I do that in C++ or VC++

    Thanks


    ps... here after a long time.
    and Canada rocks!! Peace bro.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How to tell a file is locked wityout opening (very fast)

    If you just want to know which files are open, have a look at
    http://technet.microsoft.com/en-us/s...rnals/bb896655
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Re: How to tell a file is locked wityout opening (very fast)

    Thanks for the reply, Not exactly.
    I have a list of files, I want to move them to a server, so before the process begins, I want to know if any one of those files are locked by another process. It has to be very fast, that is why dont want to open each file to see if I can obtain an exclusive open.

    Thanks
    and Canada rocks!! Peace bro.

  4. #4
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Re: How to tell a file is locked wityout opening (very fast)

    from my C++ code, I can also use MFC in my code.
    and Canada rocks!! Peace bro.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How to tell a file is locked wityout opening (very fast)

    The program 'handle' for which I gave the link will display those files which are 'open' ie cannot be moved. It is very quick. The way I do something similar to what you want to do is to redirect the output of this program into a text file which I then parse to get the file names which I store in a set. For the files I want to process, I first check if the name appears in this set and if it doesn't the file is OK to process. If the name does appear, then it isn't OK to process as the file is open by a process.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Re: How to tell a file is locked wityout opening (very fast)

    seems like a lot of overhead for what I want to do, this also means I have to ship this program now.
    Thanks... I was hoping for some API call to see if the file is locked
    and Canada rocks!! Peace bro.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to tell a file is locked wityout opening (very fast)

    There is no easy/Obvious simple solution for this other than opening the file.

    What the "handle" tool does (source is available on sysinternals) is the closest you can get if "file is opened" is all you're interested in.
    if you want to know "file is opened and has locked ranges" it becomes considerably more difficult.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How to tell a file is locked wityout opening (very fast)

    (source is available on sysinternals)
    Have you got a link to these sources? The source for some of sysinternals programs used to be available but I thought the sources had been removed a while ago?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to tell a file is locked wityout opening (very fast)

    Quote Originally Posted by IndikaNiva View Post
    I have a list of files, I want to move them to a server, so before the process begins, I want to know if any one of those files are locked by another process. It has to be very fast, that is why dont want to open each file to see if I can obtain an exclusive open.
    Could you please clarify: if *ANY* of the files are locked you will not move the other ones too?
    In that case you would have to lock the files yourself, or else they might get locked after your check but before the move.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to tell a file is locked wityout opening (very fast)

    Quote Originally Posted by 2kaud View Post
    Have you got a link to these sources? The source for some of sysinternals programs used to be available but I thought the sources had been removed a while ago?
    Aw, hadn't noticed that they removed it all. It's been a while I last wanted any of the sourcecode (I have gotten newer tools versions).

    Google is your friend, plenty mirrors have the old sources.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How to tell a file is locked wityout opening (very fast)

    OK. Found some source. To determine files that are locked the code uses some deep API functions that are not usually exported and to which the code has to dynamically link (eg NtQuerySystemInformation etc) and whose returned structures can change from one version to the next. I wouldn't like to have to maintain code like this for a production program!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured