Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to detect if a file is being used ?
#1
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 !
#2
Retry when fails

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

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

Function FileTryOpenForReadWrite
Code:
Copy      Help
;/
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
Code:
Copy      Help
;/
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
Code:
Copy      Help
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"
#3
I will try this out Smile

As always, thank you Gintaras !


Forum Jump:


Users browsing this thread: 1 Guest(s)