Results 1 to 8 of 8

Thread: watch directory

  1. #1

    Thread Starter
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180

    Question

    I need to write a program that can watch a directory. When a file appears in the directory, the program will forward it to an other directory.

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Perhaps you can use a timer program to check, say, every second or so, the directory whether there is a new file and if there is the program will transfer it to another directory. But this is an idea. I have never put it into practice.

  3. #3

    Thread Starter
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180
    I have seen an app who could watch a file or directory for changes, but i dont remember how it works or where i found it.

  4. #4
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    I haven't seen such an app. But the idea is simple and the code I don't think too difficult. The general idea is to check the directory first(using Dir$(), for example). If the directory is empty that is ok. If it is not empty then take down the names of all the files and, if necessary, some of their properties (such as length or date). Then using the timer program we can check at regular intervals to see if there is any change in the directory and/or on the files.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    i dont know but i'll bet there is an api that communicates with windows and tells you when there is a change. maybe post the question in the api forum too.
    ______________

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's something i made some months ago:
    Code:
    'Put this code in a module
    
    'Search a dir for new files
    'Calling this function the first time will only store the filenames
    'To chech after new files call again
    'Search will return a variant containing an array! Use For Each!
    Function CheckNewFiles(directory)
        
        On Error Resume Next 'oldfiles will cause subscript out of range the first time.
        Static oldfiles() As String 'This static remains in memory after first function call
        Dim newfiles() As String: ReDim newfiles(0) 'Temporary holds the new ones
        Dim foundfiles() As String: ReDim foundfiles(0) 'The array to send in return
        
        fil = Dir(directory, 7) 'Catch first file (hidden/system/archive files readen)
        Do Until fil = ""
            ReDim Preserve newfiles(UBound(newfiles) + 1) 'Resize array
            newfiles(UBound(newfiles) - 1) = fil
            For n = 0 To UBound(oldfiles) 'Search the old ones...
                If fil = oldfiles(n) Then ' ... to test if fil is old
                    Exit For
                End If
                If n = UBound(oldfiles) Then '... if no matches, add to foundlist
                    ReDim Preserve foundfiles(UBound(foundfiles) + 1) 'Resize array
                    foundfiles(UBound(foundfiles) - 1) = fil 'Add
                End If
            Next n
            fil = Dir() 'Find next file
        Loop
        
        ReDim oldfiles(UBound(newfiles) - 1) 'Removing last empty item in each array
        ReDim Preserve newfiles(UBound(newfiles) - 1)
        ReDim Preserve foundfiles(UBound(foundfiles) - 1)
        For n = 0 To UBound(newfiles) ' Store filenames until next search
            oldfiles(n) = newfiles(n)
        Next n
        CheckNewFiles = foundfiles() 'Return the found files in array
    
    End Function
    'Then in a timer on the form
    Private Sub Timer1_Timer()
        Dim n As Variant
        For Each n In CheckNewFiles("C:\Sourcedir")
           Name "C:\Sourcedir\" & n As "C:\Targetdir\" & n
        Next n
    End Sub
    + If you want it only copy the new files, you have to call CheckNewFiles before starting the timer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8

    Thread Starter
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180
    Thanks for all the tips. I havn't tried them yet, are going to do that tonight.

    Maybe its not so stupid with a timer anyway (if you're going to check every second, i think this program would be a BIG system resorce killer. But i don't need to check more often than every 15 min or so.

    Im not going to copy the file, im going to move it (thats almost the same, though).

    And, yeah, I think the other program i saw was built on an API. To sad I can't write API. Somebody who got a good turtorial on that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width