File checker program prompts after file moved or deleted

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5667

    by vanhoja at 2013-03-15 08:15:10

    I have created a powershell script/program that checks for files in 3 separate folders and prompts when a new file is in the folders so we know to process them. We then go out and work the files and either move or delete the file it prompts again. I am having problems error trapping this to only prompt when a file comes in not moved out, bellow is the code that does the checking:

    $currentFiles = Get-ChildItem C:\test
    $currentFiles2 = Get-ChildItem C:\test2
    $currentFiles3 = Get-ChildItem C:\test3

    while(-1){

    $newFiles = Get-ChildItem C:\test
    $newFiles2 = Get-ChildItem C:\test2
    $newFiles3 = Get-ChildItem C:\test3

    if($newFiles.count -ne $currentFiles.count)
    {
    $currentFiles = $newFiles
    C:\FileChecker\bin\QueuedPrompt.ps1

    }

    if($newFiles2.count -ne $currentFiles2.count)
    {
    $currentFiles2 = $newFiles2
    C:\FileChecker\bin\ErrorPrompt.ps1

    }

    if($newFiles3.count -ne $currentFiles3.count)
    {
    $currentFiles3 = $newFiles3
    C:\FileChecker\bin\EDIPrompt.ps1

    }

    Start-Sleep(5)
    }

    by DonJ at 2013-03-15 08:46:08

    You need to just change your logic a bit. Right now you're triggering when the file count CHANGES. E.g., when the count goes up or down.

    Change the logic so that you keep track of how many files there are. If the number INCREASES, trigger whatever action. If the number DECREASES, memorize that as the new baseline number, but don't do anything else.

    by vanhoja at 2013-03-16 10:25:54

    Thank you for the reply and I have been searching on it. What would be a good area to find this out? Is it in your book learn powershell in a month of lunches?

    by DonJ at 2013-03-16 10:55:45

    This is really just programming logic. You would use the same approach in any language. The book doesn't cover this explicitly, no. If there's a piece of logic you'd like me to explain in more detail I'd be happy to try.

    by vanhoja at 2013-03-17 06:28:30

    Im sorry, I have class room knowledge of programming that was last done in 2000 and this is my first job where I get to explore powershell its not part of my job but they like that I am picking it up quickly and I am enjoying it. I just don't know the code to read the event change of the files. I figured out how to check folders for new files and thought that is what the code did but found out it was checking all changes on the folder. I need help on the logic/code that is used to monitor the created event or what to search for in google which is where I find most of my info. Im still just learning Powershell only been working in it for 3 weeks and never looked at it before then.

    by vanhoja at 2013-03-17 15:45:58

    ok after taking a day off from even looking at the file I understand what you are saying but having trouble with the code.

    by mjolinor at 2013-03-17 17:09:41

    If the objective is to look for new files, I'd scrap the idea of saving and comparing lists of files.

    If you save a timestamp (using get-date),then sleep, you just need to look for files that have a timestamp newer than the one you saved when it wakes up. Those files will be new.

    by vanhoja at 2013-03-17 19:47:59

    Well I was thinking about that but we can work on a file and move it to be processed and if there is still an error it would kick back in possibly with new timestamp wouldn't it? After researching I was able to learn and change some code which I have below, it uses the filesystemwatcher and registered events. I know the production version will need to have separate folder variables and the other script that starts and stops this process I will have to code in the unregister event and error trap it but seems to work in test.

    $folder = 'c:\test' # Enter the root path you want to monitor.
    $filter = '*.txt' # You can enter a wildcard filter here.

    $fsw = New-Object IO.FileSystemWatcher $folder, $filter

    # Checks Queued Folder for new files

    Register-ObjectEvent $fsw Created -SourceIdentifier QueuedFile -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    powershell C:\FileChecker\bin\queuedprompt.ps1
    }

    # Checks Error Folder for new files

    Register-ObjectEvent $fsw Created -SourceIdentifier ErrorFile -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    powershell C:\FileChecker\bin\errorprompt.ps1
    }

    # Checks Failed Folder for new files

    Register-ObjectEvent $fsw Created -SourceIdentifier FailedFile -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated

    powershell C:\FileChecker\bin\failedprompt.ps1
    }

    by mjolinor at 2013-03-17 20:11:26

    [quote]Well I was thinking about that but we can work on a file and move it to be processed and if there is still an error it would kick back in possibly with new timestamp wouldn't it? [/quote]
    That's going to depend on your process. If the "kicking back" involves writing a new file back into the folder then yes. But the filesystem watcher will also detect that as the creation of a new file in the folder.......

    by vanhoja at 2013-03-17 20:39:59

    I think I have what I started out searching for, just a couple tweeks tomorrow at work and it should be good to test in production. Thank You DonJ and I am sorry I miss read/understood your first comment but it helped push me to keep looking and changing my search criteria. You also got me to think back to my programming training back a handful of years and able to understand more.

You must be logged in to reply to this topic.