File update

Client is sending a file in different name but FTP job looking for one file, how I can rename the client file as it in my job?

Please suggest.

Thanks
Venkatesh.
venkatesh SarivisettySenior Software EngineerAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
A sample filesystem watcher would look something like this:
Imports System.ComponentModel
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Threading

Module Module1
    '' This is where you put your directory to watch
    WithEvents watcher As New FileSystemWatcher("C:\_admin\", "*.pgp") With {.EnableRaisingEvents = True}
    WithEvents worker As New BackgroundWorker() With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
    ReadOnly fileState As New FileState() With {.Changed = DateTime.Now, .File = Nothing}
    ReadOnly queuedFiles As New List(Of ProcessableFile)
    Private isRunning As Boolean = False

    Sub Main()
        isRunning = True
        worker.RunWorkerAsync()
        Console.WriteLine("Press any key to exit")
        Console.ReadLine()
        isRunning = False
        worker.CancelAsync()
    End Sub

    Sub OnFileChanged(sender As Object, e As FileSystemEventArgs) Handles watcher.Changed, watcher.Created, watcher.Deleted
        Try
            Dim span = DateTime.Now.Subtract(fileState.Changed)
            If (span.TotalSeconds > 2 OrElse fileState.File <> e.FullPath) AndAlso e.FullPath.IndexOf("venkat.pgp", StringComparison.OrdinalIgnoreCase) = -1 Then
                Thread.Sleep(1000)
                Select Case e.ChangeType
                    Case WatcherChangeTypes.Changed
                    Case WatcherChangeTypes.Created
                        fileState.Changed = DateTime.Now
                        fileState.File = e.FullPath
                        Dim match = (From [file] In queuedFiles Where [file].File.FullName.Equals(e.FullPath, StringComparison.OrdinalIgnoreCase) Select [file]).SingleOrDefault()
                        If match IsNot Nothing Then
                            SyncLock (queuedFiles)
                                queuedFiles.Add(New ProcessableFile(New FileInfo(e.FullPath)))
                            End SyncLock
                        End If
                    Case WatcherChangeTypes.Deleted
                        fileState.Changed = DateTime.Now
                        fileState.File = e.FullPath
                        Console.WriteLine("Processed File - {0}; has been deleted.", e.FullPath)
                        Dim match = (From [file] In queuedFiles Where [file].File.FullName.Equals(e.FullPath, StringComparison.OrdinalIgnoreCase) Select [file]).SingleOrDefault()
                        If match IsNot Nothing Then
                            SyncLock (queuedFiles)
                                queuedFiles.Remove(match)
                                Console.WriteLine("{0} has been removed from the queue.", e.FullPath)
                            End SyncLock
                        End If
                    Case Else
                        Exit Select
                End Select
            End If
        Catch ex As Exception
            Console.WriteLine("Exception reported - {0}", ex)
        End Try
    End Sub

    Sub OnDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles worker.DoWork
        Try
            While (isRunning)
                SyncLock (queuedFiles)
                    For i As Integer = queuedFiles.Count - 1 To 0 Step -1
                        If Not queuedFiles(i).IsProcessed Then
                            Console.WriteLine("Processing item - {0}", queuedFiles(i).File.FullName)
                            queuedFiles(i).TryProcess()
                        Else
                            If queuedFiles(i).IsMatched Then
                                ' Handle your matched files (e.g. - Delete)
                                Console.WriteLine("Deleting matched file - {0}", queuedFiles(i).File.FullName)
                                queuedFiles(i).File.Delete()
                            Else
                                ' Continue with your normal process
                                Console.WriteLine("Unmatched file - {0}; has been removed from the queue.", queuedFiles(i).File.FullName)
                                queuedFiles.Remove(queuedFiles(i))
                            End If
                        End If
                    Next
                End SyncLock
            End While
        Catch ex As Exception
            Console.WriteLine("Exception reported - {0}", ex)
        End Try
    End Sub
End Module

Class FileState
    Public Property Changed() As DateTime
    Public Property File() As String
End Class

Class ProcessableFile
    ReadOnly _file As FileInfo
    ReadOnly _lastUpdated As DateTime
    Private _isMatched As Boolean = False
    Private _isProcessed As Boolean = False
    Private _isProcessing As Boolean = False

    Public ReadOnly Property File() As FileInfo
        Get
            Return _file
        End Get
    End Property

    Public ReadOnly Property IsMatched() As Boolean
        Get
            Return _isMatched
        End Get
    End Property

    Public ReadOnly Property IsProcessing() As Boolean
        Get
            Return _isProcessing
        End Get
    End Property

    Public ReadOnly Property IsProcessed() As Boolean
        Get
            Return _isProcessed
        End Get
    End Property

    Public Sub New([file] As FileInfo)
        _file = [file]
        _lastUpdated = DateTime.Now
        _isProcessing = True
    End Sub

    Public Sub TryProcess()
        Try
            If _isProcessing Then
                '' This is the regex that states to match the files with numbers and letters regardless of case, if you want only digits use:
                '' ^[0-9]*$
                If Regex.IsMatch(Path.GetFileNameWithoutExtension(_file.Name), "^(?i)[a-z0-9]*$") Then
                    _file.CopyTo(Path.Combine(_file.DirectoryName, "Venkat.pgp"), True)
                    _isMatched = True
                End If
                _isProcessed = True
                _isProcessing = False
            End If
        Catch ex As Exception
            Console.WriteLine("Exception reported - {0}", ex)
        End Try
        End Sub6
End Class

Open in new window


-saige-
0
 
Leonidas DosasCommented:
Can you rename the uploaded file AFTER the uploading? I mean to set a new name with numerical names like upload1,upload2,...etc
0
 
venkatesh SarivisettySenior Software EngineerAuthor Commented:
I cant rename, becoz client is sending like 1234.pgp and I am renaming like venkat.pgp.

Once I renamed the file, it will get match with my Job and it will pick the file to proceed furhter. Please advise
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
venkatesh SarivisettySenior Software EngineerAuthor Commented:
any suggestions please .. it is getting failed daily. Need to fix it permanently. As Client not able to send the with file name, is there a way I can rename before my job got triggered. Please share your thoughts.

THANKS
venkatesh
0
 
it_saigeDeveloperCommented:
You have not let us know how your process works (no code provided) nor have you specified why the client file must match the name you provided.  The simplest answer would be to change your process to accept any .pgp file.

-saige-
0
 
venkatesh SarivisettySenior Software EngineerAuthor Commented:
Hi Saige,

Actually it is File FTP transfer program,

Our Job will go and see the client location with the name called Venkat.pgp daily, but everyday client is sending the file as 1234.pgp(any other numbers). So the job is looking for venkat.pgp and getting failed.

How we can update the 1234.pgp file to Venkat.pgp before job looking for the file. Also as you said, if I give any .pgp file to pick.. it will pick other files which are avilable in the client source path which is not required.

Please advie.

Thanks
Venkatesh.
0
 
it_saigeDeveloperCommented:
Do other clients use the same source path or just this one client?  Other just this one client uses this source path, rather than changing the file names at all, create a folder for completed files and move the processed file to this completed folder as the last thing the job does...  Then all you have to do is accept any .pgp file.  Otherwise, what you will need to do is setup a file watcher for the source location and change the files as they are dropped.

-saige-
0
 
venkatesh SarivisettySenior Software EngineerAuthor Commented:
Yes Saige, I liked the file watcher , can you please provide me the code snippet to monitor any files coming with Numbers into the source path. Once it came, it can renamed to Venkat.pgp. So, that the Job wont get any impact. Your help is much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.