Hi,
Really late entry to this post. But I was having a random problem opening files. Writing an explorer type program, with an added light security feature where part of a file can be replaced (designed for large files, where standard decryption would take too long when accessing the file). As part of the testing, I use a batch file called from within the code (lazy sod) to copy the original test files into the working folder, since once the files are converted, they no longer exist in their original form. I was getting this random problem on the open. I noted that some of these had that annoying block flag Windows sometimes adds to files. I used powershell to clear that (another forum entry, where was the internet when I was programming in the 80s). That wasn't the problem.
I am using stream, yes I know the original poster was using good old Pascal I/O, but this illustrates that it is a Windows issue, not the way the file was opened. I could not see any way to check if the file was open already, so just had to rely on open failing. The open is in a Boolean function in my file unit.
Anyway, I added a very simple For loop around the file open, added the Sleep (half a second, based on the timing given by marcov and seems a reasonable figure) and only if fails 3 times will it activate the error system.
{ my file unit code all has p_file__ at the front, so I know at a glance which unit any item is in, open_file and can_write had that removed to make easier to view
open_file is simply TFilestream.Create inside an Except plus check (FileExists)
can_write is just a boolean constant to tell the function to open in read & write mode, default is read only
full_path_file is gained from the list obtained by FindAllFiles, load_stream is the TStream
}
For i := 1 To 3 Do Begin // arbitrary, but 3 was similar to marcov and seems a sensible number
If open_file( load_stream, full_path_file, can_write ) Then Goto set_details; // I like Goto, I'm old school, within small procedures they provide a neat flow solution
Sleep( 500 ); // wait half a second for any ops to clear
End;
// error routine
I wanted to add that very simple solution for anyone affected by the same issue, as it clearly is a Windows issue that has not gone away. I'm using Windows 10 Professional.
I'd guess what is happening is that maybe because the original files are on another drive, that when I copy them to the C drive, Windows (maybe the antivirus, Avira), wants to check the files out and because I call the batch file from within the code, that by the time the program reaches the files, the operating system is still dicking about. I hadn't got it to run more than once in a row without the error before the little add, just ran it 5 times in a row with no errors.
Thank you to the regular contributors, Jamie, Thaddy and marcov.
Phil (aka old DOS err)
Lazarus: 3.4 (date: 2024-05-25) FPC: 3.2.2 Version: Win64
Operating system: Windows Pro 10, 22H2
Hardware: Intel Core i5-3570K CPU, 3.40GHz, RAM: 8GB, 64 bit