IsFileReady
-
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.
-
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.
Wait on the directory change notification ReadDirectoryChangesW function (winbase.h) - Win32 apps | Microsoft Docs[^] This might help GitHub - LdB-ECM/FolderWatcher[^]
In vino veritas
-
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.
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)
-
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)
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
-
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.
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
-
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.
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.
-
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.
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.
-
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.
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.