AdvancedHMI Software

General Category => Open Discussion => Topic started by: bachphi on July 25, 2017, 09:29:49 PM

Title: Check If File Exist
Post by: bachphi on July 25, 2017, 09:29:49 PM
In certain machine, when cycle is complete, a result data file is generated.
The file is later processed and inserted into a production database.
Assuming the file is generated in the 'Result' folder, we move it to another folder 'XferedData' for processing.

One might ask, why dont we watch for files directly in the 'Result' folder? The reason for that is if we have to shutdown our application and the machine continue to generates files, those files will be ignored when the application is resumed.

Using a timer, we check if file exist in 'Result', if so move to 'XferedData' folder. A FileSystemWatcher will watch for file creation in 'XferedData' and process it accordingly. http://advancedhmi.com/forum/index.php?topic=1798.msg9980#msg9980 (http://advancedhmi.com/forum/index.php?topic=1798.msg9980#msg9980)

Since using a timer is not really my favorite, I would prefer to have an event driven, but not sure how to. TIA.

Code: [Select]
Private Sub CheckIfFilesExist()
        Try
            Dim sourceDir As String = "E:\Temp2\Result"
            'Check if file(s) exist , if so move to dest. dir for processing
            Dim files As String() = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories)

            If (files.Length > 0) Then
                For Each Fil As String In files
                    Dim destFile As String = String.Format("{0}\{1}", destDir, Path.GetFileName(Fil))
                    System.IO.File.Move(Fil, destFile)
                Next
            End If
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub
Title: Re: Check If File Exist
Post by: Phrog30 on July 25, 2017, 10:21:15 PM
Could you use a counter? When cycle complete you increment the counter. The change on this counter is your event. You can store this in app data or ini file so you always know where you left off and this would satisfy if the app stopped.
Title: Re: Check If File Exist
Post by: bachphi on July 26, 2017, 07:31:22 AM
There is no communication between machine & application. The application may even reside in a different PC.
Title: Re: Check If File Exist
Post by: Phrog30 on July 26, 2017, 05:52:00 PM
Ok, not even kinda following then. Best of luck.
Title: Re: Check If File Exist
Post by: bachphi on October 08, 2017, 06:31:06 PM
Sometime ago, in the first post , I indicated that I have two folders, I further explained why I needed to have two folders. Using a timer, I moved results from 1st to 2nd folder, a file watcher will then process the file to DB.

I now realized that there is no need for that, the application would be watching for just one 'result' folder, and it would create a thread to check for pre-existing files and if there are any upon starting up , processing them as needed. A file watcher will continue to watch for new files as they are created.