How to detect if a file is being used ?

Creating macros. Working with QM.

How to detect if a file is being used ?

Postby Extreme on Sun Oct 01, 2017 12:16 pm

Hello !

With QM i've programmed a macro that scans a specific folder for specific files, and then move then to another specific folder.

Exemple : QM scans D:\Folder1-> QM Detects a new file in D:\Folder1-> It moves it automatically to D:\Folder2

The issue here is that when files are being downloaded to D:\Folder1 , they are obviously used by another process for the download ( chrome, bitorrent... ) so they can't be moved, and i can't figure how QM can detect this.

The ideal solution would be :

QM scans D:\Folder1-> QM Detects a new file that is not used by any process in D:\Folder1-> It moves it automatically to D:\Folder2

Is there a clean way to do this ?

Thanks !
Extreme
 
Posts: 19
Joined: Thu Oct 02, 2014 11:43 am

Re: How to detect if a file is being used ?

Postby Gintaras on Sun Oct 01, 2017 5:23 pm

Retry when fails

;g1
FileMove from to
err
,if(!FileExists(from)) ret
,wait 0.5; goto g1



----------------------------------
or use one of these functions

Function FileTryOpenForReadWrite     ?
;/
function# $filePath

;Returns 0 if the specified file exists and can be opened for read/write access (and can be moved etc).
;Else returns an error code.
;Some error codes:
;;;ERROR_FILE_NOT_FOUND (2) - the file does not exist.
;;;ERROR_PATH_NOT_FOUND (3) - the directory does not exist.
;;;ERROR_ACCESS_DENIED (5) - read-only or protected.
;;;ERROR_SHARING_VIOLATION (32) - is already open, for example by another process.


_s.expandpath(filePath)
int h=CreateFileW(@_s GENERIC_READ|GENERIC_WRITE 0 0 OPEN_EXISTING FILE_ATTRIBUTE_NORMAL 0)
if(h==INVALID_HANDLE_VALUE) ret GetLastError
CloseHandle h


Function FileTryOpenForRead     ?
;/
function# $filePath

;Returns 0 if the specified file exists and can be opened for read access.
;Else returns an error code.
;Some error codes:
;;;ERROR_FILE_NOT_FOUND (2) - the file does not exist.
;;;ERROR_PATH_NOT_FOUND (3) - the directory does not exist.
;;;ERROR_SHARING_VIOLATION (32) - is already open, for example by another process.


_s.expandpath(filePath)
int h=CreateFileW(@_s GENERIC_READ 0 0 OPEN_EXISTING FILE_ATTRIBUTE_NORMAL 0)
if(h==INVALID_HANDLE_VALUE) ret GetLastError
CloseHandle h



example
out
str s="q:\test\file.txt"
sel FileTryOpenForReadWrite(s)
,case 0 out "can open and move"
,case ERROR_SHARING_VIOLATION out "cannot open, it is used by something"
,case [2,3] out "does not exist"
,case else out "cannot open, unknown error"

Gintaras
Site Admin
 
Posts: 10386
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: How to detect if a file is being used ?

Postby Extreme on Sun Oct 01, 2017 9:36 pm

I will try this out :)

As always, thank you Gintaras !
Extreme
 
Posts: 19
Joined: Thu Oct 02, 2014 11:43 am


Return to General

Who is online

Users browsing this forum: No registered users and 3 guests