Jump to content

How to watch a folder?


Recommended Posts

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.

Link to comment
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

Link to comment
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
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
Link to comment
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
Link to comment
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()
Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...