Sign in to follow this  
Followers 0
pete1234

How to watch a folder?

9 posts in this topic

I'm looking to make a script that will watch an empty folder. Whenever a file enters the folder, I'd like the script to move it to another directory and timestamp it. The last part is obviously easy, but I'm not sure how to tackle the first part. Would I need to create a service or something along those lines?

Thanks for any ideas.

Share this post


Link to post
Share on other sites



I'm looking to make a script that will watch an empty folder. Whenever a file enters the folder, I'd like the script to move it to another directory and timestamp it. The last part is obviously easy, but I'm not sure how to tackle the first part. Would I need to create a service or something along those lines?

Thanks for any ideas.

Have a look at FileFindFirstFile (). Call that in a loop, and just test what it returns. "Failure: Returns -1 if error occurs. If the Folder is empty the @error is set to 1. "

In the loop, check it every 30 seconds or so, as not to chew up CPU.

If you need any more help, post your code, and we'll see muttley

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#Include <File.au3>
$Folder = "C:\Users\USER\Desktop\Watch Folder"
$DestinationDir = "C:\Users\USER\Desktop\Other Folder"

While 1
$FileArray = _FileListToArray($Folder)
If Not $FileArray[0] = 0 Or $FileArray[0] = "0" Then
FileMove($Folder&"\"&$FileArray[1], $DestinationDir&"\"&$FileArray[1])
;timestamp it
EndIf
WEnd

^^^ The above code is not tested. I will test it as soon as I can, this should give you a small example to go with.

EDIT: @Bert, I was thinking FileFindFirstFile, but wasn't 100% sure.

Edited by Alienware

Share this post


Link to post
Share on other sites

#Include <File.au3>
$Folder = "C:\Users\USER\Desktop\Watch Folder"
$DestinationDir = "C:\Users\USER\Desktop\Other Folder"

While 1
$FileArray = _FileListToArray($Folder)
If Not $FileArray[0] = 0 Or $FileArray[0] = "0" Then
WinMove($Folder&"\"&$FileArray[1], $DestinationDir&"\"&$FileArray[1])
;timestamp it
EndIf
WEnd

^^^ The above code is not tested. I will test it as soon as I can, this should give you a small example to go with.

You have got to be joking. Since when did WinMove move files and folders?

Something like this would work, still untested. You said you knew what to do with the file move and timestamp, so I just put a message box there... muttley

$watching = "C:\Watching\*.*" 
$moving = "C:\Move\Me\Here\" 
$timetowatch = 30 * 1000
$timestamp = TimerInit()

While 1
    If TimerDiff($timestamp) >= $timetowatch Then
        $check = FileFindFirstFile($watching)
        If $check <> -1 Then
            While 1
                $file = FileFindNextFile($check)
                If @error Then ExitLoop
                MsgBox(4096, "File:", $file)
            WEnd
        EndIf
        $timestamp = TimerInit()
    EndIf
    Sleep (20)
WEnd

Share this post


Link to post
Share on other sites

You have got to be joking. Since when did WinMove move files and folders?

Something like this would work, still untested. You said you knew what to do with the file move and timestamp, so I just put a message box there... muttley

$watching = "C:\Watching\*.*" 
$moving = "C:\Move\Me\Here\" 
$timetowatch = 30 * 1000
$timestamp = TimerInit()

While 1
    If TimerDiff($timestamp) >= $timetowatch Then
        $check = FileFindFirstFile($watching)
        If $check <> -1 Then
            While 1
                $file = FileFindNextFile($check)
                If @error Then ExitLoop
                MsgBox(4096, "File:", $file)
            WEnd
        EndIf
        $timestamp = TimerInit()
    EndIf
    Sleep (20)
WEnd
Yeah, oops, I meant FileMove()

Share this post


Link to post
Share on other sites

Boy, that was quick! Thanks for the replies, I will do some testing later tonight or tomorrow and report back.

Share this post


Link to post
Share on other sites

Also, if he searches some in the forum, there's a WMI method, to detect file related actions, which he could use, then compare the path to the file in question, and see if it matches his path, then take action from there(this would be my preferred method of approach(even tho it's a lil bit advanced)). muttley

Share this post


Link to post
Share on other sites

^ I saw a few of the file monitoring examples which I believe you are referring to. They look pretty cool, but my knowledge of AutoIt is not that advanced yet.

Anyways, this is what I came up with in regards to the script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0