When is the filesystem ready?

Windows specific forum
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

When is the filesystem ready?

Post by BarryG »

On Windows, when copying or moving files to a folder (from outside PureBasic), I need to know when there's no file activity going on inside it. So like if I do a file copy on large file in Explorer with Ctrl+C (source) and Ctrl+V (paste), I want my app to know when it's complete. How can I do that? I can't check with FileSize() because it's not done from PureBasic, and also, when the copy is done, it takes a moment for the file to actually appear in the folder.
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: When is the filesystem ready?

Post by ebs »

Maybe some of this code would help?

viewtopic.php?p=339666
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: When is the filesystem ready?

Post by BarryG »

Hi ebs, unfortunately those monitoring tips can't help because when a large file is added, I get the trigger about it being added but the file is nowhere near finished copying, as shown below. That's my problem. I need to know when the file is finished copying, and for multiple files, so that there is no more file activity of any kind in the folder. Thanks anyway for the idea!

Image
User avatar
ChrisR
Addict
Addict
Posts: 1151
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: When is the filesystem ready?

Post by ChrisR »

I had solved a similar problem using ts-soft CopyDirEx - Module
With it, FileSize remains available and thus, my progress bar continues to advance while copying large files.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: When is the filesystem ready?

Post by BarryG »

Thanks, but PureBasic is not copying the files (see my first post).
User avatar
Caronte3D
Addict
Addict
Posts: 1054
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: When is the filesystem ready?

Post by Caronte3D »

Maybe you could try to open the file to know if it's locked by Windows?
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: When is the filesystem ready?

Post by BarryG »

Can't do that, because some folders have thousands of files to open/check, such as my MP3 folder. Can't check the lock on all those just to see if any new file activity is going on in there.
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: When is the filesystem ready?

Post by Bitblazer »

Maybe Sync can help?
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: When is the filesystem ready?

Post by BarryG »

I'll check out Sync more closely tomorrow, but it needs admin rights to run (limits its use) and Win 10 also gave an alert that it stopped it modifying memory, which was scary to see. Not sure I want to depend on a third-party tool, either (its EULA prevents commercial use). Thanks for suggesting it, though!
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: When is the filesystem ready?

Post by Keya »

There will always be an open handle to the file while it is being written, so you can detect this by enumeration of handles (NtQueryProcessInformation)
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: When is the filesystem ready?

Post by BarryG »

Can't check individual file handles, as I said. There could be thousands of them. And if one file is huge (like my example above), then I'd have to keep polling its handle at regular intervals before moving onto the next file. This approach is totally inefficient.

There doesn't seem to be a simple "this dir has activity" API check, so I guess it's a lost cause.
Bitblazer
Enthusiast
Enthusiast
Posts: 736
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: When is the filesystem ready?

Post by Bitblazer »

The original question still has different solutions - depending on what exactly you want to know. But i still think sync is the answer. Peek into the sync source and you will see the windows api call you look for. Or just use runprogram and call sync.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: When is the filesystem ready?

Post by Keya »

There doesn't seem to be a simple "this dir has activity" API check, so I guess it's a lost cause.
see https://docs.microsoft.com/en-us/window ... ifications
An application can specify a set of conditions that trigger a change notification by using the FindFirstChangeNotification API function (and then FindNextChangeNotification in a loop). The conditions include changes to file names, directory names, attributes, file size, time of last write, and security.

To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function. This function also enables you to provide a completion routine.

(you'll probably want to call them from inside another thread, as FindFirstChangeNotification is a blocking function - it will hang your thread until a notification event is triggered, and while it's likely it will happen fairly quickly there is no actual guarantee that it will ever happen)

Some PB examples to help get you started:
https://www.purebasic.fr/english/viewtopic.php?t=14776
viewtopic.php?f=5&t=74611
https://www.cyberforum.ru/pure-basic/thread1520399.html
Post Reply