Private Sub fsWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsWatcher.Created
'If a new file is created in the folder that is being monitored,
'Then this code will execute
'Write activity to log
txtActivity.Text = txtActivity.Text & DateAndTime.Now & " File Created: " & e.FullPath & vbNewLine
writeToLogFile(DateAndTime.Now & " File Created: " & e.FullPath)
txtActivity.Update()
'Determine long and truncated filenames
fullFilename = e.FullPath
shortFilename = Mid$(fullFilename, InStrRev(fullFilename, "\") + 1)
truncatedFilename = Split(shortFilename, "_")
If shortFilename.Substring(Len(shortFilename) - 3) = "jpg" Or shortFilename.Substring(Len(shortFilename) - 3) = "JPG" Or shortFilename.Substring(Len(shortFilename) - 4) = "JPEG" Or shortFilename.Substring(Len(shortFilename) - 4) = "jpeg" Then
Application.DoEvents()
StatusBar.Panels(0).Text = "File found! Starting Renaming Process..."
ElseIf shortFilename.Substring(Len(shortFilename) - 3) = "CSV" Or shortFilename.Substring(Len(shortFilename) - 3) = "csv" Then
txtActivity.Text = txtActivity.Text & "*** Log File Found. Moving to log directory... ***" & vbNewLine
writeToLogFile("*** Error: Invalid file detected. Moved to Exception Directory. ***")
IO.File.Move(fullFilename, csvFilePath & "\" & shortFilename)
Exit Sub
Else
txtActivity.Text = txtActivity.Text & "*** Error: Invalid file detected. Moved to Exception Directory. ***" & vbNewLine
writeToLogFile("*** Error: Invalid file detected. Moved to Exception Directory. ***")
IO.File.Move(fullFilename, selectedExceptionPath & "\" & shortFilename)
Exit Sub
End If
'Split filename into different parts
sOrder = truncatedFilename(0)
sBranch = truncatedFilename(1)
sDateTime = truncatedFilename(2)
sPicNum = truncatedFilename(3)
nDate = sDateTime.Substring(0, 6)
nTime = sDateTime.Substring(6, 6)
tmpDate = nDate.Insert(2, "-")
finalDate = tmpDate.Insert(5, "-")
'Determine from the branch number the state or city abbreviation
Select Case sBranch
Case "01"
branchName = "Corp"
Case "02"
branchName = "PA"
Case "03"
branchName = "GA"
Case "04"
branchName = "MA"
Case "05"
branchName = "CO"
Case "06"
branchName = "MO"
Case "07"
branchName = "SF"
Case "08"
branchName = "TX"
Case "09"
branchName = "LA"
Case "10"
branchName = "FL"
End Select
'Store new filename into variable
newFilename = branchName & "_" & finalDate & "_" & sPicNum
'Log current activity
txtActivity.Text = txtActivity.Text & DateAndTime.Now & " Begin File Renaming: " & e.FullPath & vbNewLine
writeToLogFile(DateAndTime.Now & " Begin File Renaming: " & e.FullPath)
txtActivity.Update()
Dim fullNewFilename As String
'Determine full new filename (i.e. C:\input\fl_5-22-06_03.jpg)
fullNewFilename = selectedPath & "\" & newFilename
'Lets the files finish writing before being accessed by the scanning program
Pause(100)
'Rename files
If IO.File.Exists(fullNewFilename) = True Then
txtActivity.Text = txtActivity.Text & "*** Error: File exists in Monitoring Directory! File " & fullFilename & " has not been renamed. *** " & vbNewLine
writeToLogFile("*** Error: File exists in Monitoring Directory! File " & fullFilename & " has not been renamed. *** ")
txtActivity.Update()
Else
If IO.File.Exists(fullNewFilename) = True Then
txtActivity.Text = txtActivity.Text & "*** Error writing file " & fullNewFilename & ". File already exists! ***" & vbNewLine
writeToLogFile("*** Error writing file " & fullNewFilename & ". File already exists! ***")
txtActivity.Update()
Else
MsgBox("Full Filename: " & fullFilename & vbNewLine & "Full New Filename: " & fullNewFilename)
IO.File.Move(fullFilename, fullNewFilename)
End If
End If
'Database Code here
End Sub