Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. IsFileReady

IsFileReady

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpcareer
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erich Ruth
    wrote on last edited by
    #1

    I use this code:

    Quote:

    BOOL C1095C::IsFileReady(CString path) { HANDLE hFile = CreateFile(path.GetBuffer(), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } else { CloseHandle(hFile); return TRUE; } }

    while (!IsFileReady(output.GetBuffer())) { Sleep(1); } which waits for a file to be created and then once it is, it proceeds to the next job. The problem is, this code doesnt seem to work every time. Im nervous now in using it. I need to wait for a file to be full created and sometimes it may take 5+ minutes. Once its created, then I want to move onto the next step. How can I adjust this code to make it work every time? Please any response any one can give me will be great appreciated.

    L S D D 4 Replies Last reply
    0
    • E Erich Ruth

      I use this code:

      Quote:

      BOOL C1095C::IsFileReady(CString path) { HANDLE hFile = CreateFile(path.GetBuffer(), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } else { CloseHandle(hFile); return TRUE; } }

      while (!IsFileReady(output.GetBuffer())) { Sleep(1); } which waits for a file to be created and then once it is, it proceeds to the next job. The problem is, this code doesnt seem to work every time. Im nervous now in using it. I need to wait for a file to be full created and sometimes it may take 5+ minutes. Once its created, then I want to move onto the next step. How can I adjust this code to make it work every time? Please any response any one can give me will be great appreciated.

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      Wait on the directory change notification ReadDirectoryChangesW function (winbase.h) - Win32 apps | Microsoft Docs[^] This might help GitHub - LdB-ECM/FolderWatcher[^]

      In vino veritas

      1 Reply Last reply
      0
      • E Erich Ruth

        I use this code:

        Quote:

        BOOL C1095C::IsFileReady(CString path) { HANDLE hFile = CreateFile(path.GetBuffer(), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } else { CloseHandle(hFile); return TRUE; } }

        while (!IsFileReady(output.GetBuffer())) { Sleep(1); } which waits for a file to be created and then once it is, it proceeds to the next job. The problem is, this code doesnt seem to work every time. Im nervous now in using it. I need to wait for a file to be full created and sometimes it may take 5+ minutes. Once its created, then I want to move onto the next step. How can I adjust this code to make it work every time? Please any response any one can give me will be great appreciated.

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        Is that Linux sleep() or Windows Sleep()? Or, more to the point, are you sleeping for 1 second, or 1 millisecond? If the latter, maybe you should try to increase the time to around 100ms: The Windows file system is not very fast, and might be hamstrung by constantly trying to open nonexistant files.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        L 1 Reply Last reply
        0
        • S Stefan_Lang

          Is that Linux sleep() or Windows Sleep()? Or, more to the point, are you sleeping for 1 second, or 1 millisecond? If the latter, maybe you should try to increase the time to around 100ms: The Windows file system is not very fast, and might be hamstrung by constantly trying to open nonexistant files.

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          It's clearly windows no such thing as CreateFile in linux and he already stated it can take 5min to open the file (I assume of a network drive) so 100ms is not going to change anything :-)

          In vino veritas

          1 Reply Last reply
          0
          • E Erich Ruth

            I use this code:

            Quote:

            BOOL C1095C::IsFileReady(CString path) { HANDLE hFile = CreateFile(path.GetBuffer(), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } else { CloseHandle(hFile); return TRUE; } }

            while (!IsFileReady(output.GetBuffer())) { Sleep(1); } which waits for a file to be created and then once it is, it proceeds to the next job. The problem is, this code doesnt seem to work every time. Im nervous now in using it. I need to wait for a file to be full created and sometimes it may take 5+ minutes. Once its created, then I want to move onto the next step. How can I adjust this code to make it work every time? Please any response any one can give me will be great appreciated.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            In addition to Leon's suggestion, there's also FindFirstChangeNotification().

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • E Erich Ruth

              I use this code:

              Quote:

              BOOL C1095C::IsFileReady(CString path) { HANDLE hFile = CreateFile(path.GetBuffer(), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return FALSE; } else { CloseHandle(hFile); return TRUE; } }

              while (!IsFileReady(output.GetBuffer())) { Sleep(1); } which waits for a file to be created and then once it is, it proceeds to the next job. The problem is, this code doesnt seem to work every time. Im nervous now in using it. I need to wait for a file to be full created and sometimes it may take 5+ minutes. Once its created, then I want to move onto the next step. How can I adjust this code to make it work every time? Please any response any one can give me will be great appreciated.

              D Offline
              D Offline
              Daniel Pfeffer
              wrote on last edited by
              #6

              If you are waiting for the file to be fully created (i.e. completely written), then this code won't work. At best, it will wait until the file is created (i.e. when the other task opens it for writing). The problem may be broken into two steps: 1. Wait until the file is created (i.e. it appears in the directory) 2. Wait until the file is closed (i.e. the writing is completed) Others have given you good ideas about how to wait for the file to be created. In order to wait for the file to be completely written, the best way would be for the other application to create another file AFTER it is done with the data file. For example: 0. Your application waits for the creation of DATA.DAT 1. Other application creates file DATA.DAT (your application detects the creation) 2. Other application writes into DATA.DATA (your application waits for the creation of FINISHED.DAT) 3. Other application closes DATA.DAT 4. Other application creates FINISHED.DAT (your application detects FINISHED.DAT, and knows that DATA.DAT is ready) There are some other points to handle (e.g. what happens if DATA.DAT is created before your application starts waiting for it?), but this is the basic idea.

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              J 1 Reply Last reply
              0
              • D Daniel Pfeffer

                If you are waiting for the file to be fully created (i.e. completely written), then this code won't work. At best, it will wait until the file is created (i.e. when the other task opens it for writing). The problem may be broken into two steps: 1. Wait until the file is created (i.e. it appears in the directory) 2. Wait until the file is closed (i.e. the writing is completed) Others have given you good ideas about how to wait for the file to be created. In order to wait for the file to be completely written, the best way would be for the other application to create another file AFTER it is done with the data file. For example: 0. Your application waits for the creation of DATA.DAT 1. Other application creates file DATA.DAT (your application detects the creation) 2. Other application writes into DATA.DATA (your application waits for the creation of FINISHED.DAT) 3. Other application closes DATA.DAT 4. Other application creates FINISHED.DAT (your application detects FINISHED.DAT, and knows that DATA.DAT is ready) There are some other points to handle (e.g. what happens if DATA.DAT is created before your application starts waiting for it?), but this is the basic idea.

                Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #7

                IF you know that this will be on a local drive, an alternative is to create the file in a different directory and then move it to the target directory.

                D 1 Reply Last reply
                0
                • J Joe Woodbury

                  IF you know that this will be on a local drive, an alternative is to create the file in a different directory and then move it to the target directory.

                  D Offline
                  D Offline
                  Daniel Pfeffer
                  wrote on last edited by
                  #8

                  Subject to the limitation you mentioned, that would work, too.

                  Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups