"FileMove" a file which is currently created

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

"FileMove" a file which is currently created

16 Dec 2014, 05:59

I have a folder which is currently filled with files from another software. AHK watches the folder and starts a FileMove if it finds files.

Of course it could happen that a file is still "under construction" (still copying from other software) and AHK starts to "FileMove". What will happen?

- an errorlevel and a skipped file?
- an errorlevel and a fragmented / destroyed source/target?
- an internal "retry until success"?

Thanks and regards
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: "FileMove" a file which is currently created

18 Dec 2014, 19:54

most likely 2 but 1 can also happen
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: "FileMove" a file which is currently created

19 Dec 2014, 09:53

TLM wrote:most likely 2 but 1 can also happen
:shock: :o

Thanks for your info.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: "FileMove" a file which is currently created

19 Dec 2014, 15:54

There are often ways of determining when a file has been copied or saved by a source application.
The most common is a status bar/control message, others may be some kind of single blocking action like disabling the ability to copy more files while one is being processed or even the change of style codes on main windows or controls after a file is complete.
Perhaps you could find some indicator in the source app ? Just a thought
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: "FileMove" a file which is currently created

20 Dec 2014, 03:41

Wrote a little example to see if the file size has changed in the last two seconds. If the filesize it the same; it's probably done being created. Increase the time if it builds slower than that.

Code: Select all

FileGetSize, OutputVar1 , %FilePath_File%, k
Sleep 2000
FileGetSize, OutputVar2 , %FilePath_File%, k
	If (OutputVar1 = OutputVar2) {
	;File hasn't changed size in 2 seconds, must be done being constructed
	FileMove, %FilePath_File%, %DestPattern%
	}
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: "FileMove" a file which is currently created

20 Dec 2014, 15:57

Hi

thanks both for your postings. I think that both solutions can not be used for my problem, because

a) I have no connection to the source app; I'm only the receiver of the files at the end of the network-pipe.
b) Sleeping could work, but with some 1000s of files per day the delay would be unacceptable.

Also the way "copy source to temp - compare source with temp - if source is equal to temp then source is OK - move source to target and delete temp" has to prove its usability ...
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: "FileMove" a file which is currently created

20 Dec 2014, 19:11

Didn't know this was being applied to 1000's of files.

Would recommend one of two things.

1) Use an array to keep track of each file and its size,size_check2,current_location,destination,time
2) Use Robocopy to handle all of your file moving; fairly confident it will not interact with a file that is still being written. Check the options and do some tests.
If it takes multiple days; there is an option to only move/copy the file after x days.


If you know how to use arrays or can learn; here's part of my production tool that monitors filesizes; I threw a filemove in there for your use. I only monitor 4 files but there would be no problem filling the array with 1000+ files to handle.

Code: Select all

Loop % AllFiles_Array.MaxIndex() {

FilePath_File := AllFiles_Array[A_Index,"FileLocation"]

;Get Modified Time and assign it to the Array
AllFiles_Array[A_Index,"ModifiedTime"] := Fn_DataFileInfoTime(FilePath_File)

;Get File Size and assign it to the Array
AllFiles_Array[A_Index,"Size"] := Fn_DataFileInfoSize(FilePath_File)


	;If the Filesize is the same as last time it was checked
	If (AllFiles_Array[A_Index,"LastCheck"] = AllFiles_Array[A_Index,"Size"]) {
	;Then increment the Counter for not growing
	AllFiles_Array[A_Index,"NotGrowingCounter"] += 1
	FileMove, %FilePath_File%, %DestPattern%
	} Else {
	;Do nothing
	}
	
AllFiles_Array[A_Index,"LastCheck"] := AllFiles_Array[A_Index,"Size"]
}



;~~~Functions~~~
Fn_DataFileInfoSize(para_File)
{
l_FileSize := ;MakeThis Variable Empty

;Check the size of the file specified in the Function argument/option
FileGetSize, l_FileSize, %para_File%, k

	;If the filesize is NOT blank
	If (l_FileSize != "")
	{
	;Exit the Function with the value of the filesize
	Return %l_FileSize%
	}
;filesize was blank or not understood. Return the word "ERROR"
Return "ERROR"
}
Guest

Re: "FileMove" a file which is currently created

21 Dec 2014, 09:56

If you just check when the file was last modified using FileGetTime and only move files that haven't been modified in the last few minutes (or whatever length of time is appropriate), then there is no need for your script to sleep or anything.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Spawnova, william_ahk and 259 guests