轮询目录以查找具有特定扩展名的文件

时间:2012-11-23 10:14:41

标签: vb.net

我正在编写一个脚本来查看目录,读取文件名并使用文件名的一部分来运行SQL查询来修改数据库,然后将文件复制到新位置并删除原始文件。

完成此操作后,它会向预定义的电子邮件地址发送电子邮件确认。

我占多数,但我无法轮询Dir并处理可能存在的所有文件。我是这个VB.net的新手,并且在开始时将其他工作用于iv。

非常感谢任何帮助。

    Dim fileName As String = "C:\temp\Input\VBTEST1.success"
    Dim pathname As String = "C:\temp\Input\"
    Dim result As String
    Dim sourceDir As String = "C:\temp\Input\"
    Dim processedDir As String = "C:\temp\Input\Processed\"
    Dim fList As String() = Directory.GetFiles(sourceDir, "*.success")
    Dim sqlCommand As SqlCommand

    Public Sub Main()
      result = Path.GetFileName(fileName)

      Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result)

      Dim betacell As String = result
      betacell = (result.Remove(7, 8))

      Dim connection As New SqlConnection(My.Settings.connectionString)
      connection.Open()

      Dim updateTransaction As SqlTransaction = connection.BeginTransaction()
      Dim sqlQ As String = "UPDATE " & My.Settings.JobTb & " SET Status = '10'  WHERE  JobNumber ='" & betacell & "'"

      sqlCommand = New SqlCommand(sqlQ, connection, updateTransaction)

      sqlCommand.ExecuteNonQuery()
      updateTransaction.Commit()
      connection.Close()
      SendEmail(My.Settings.emailUsers, "EMAIL TEXT")
      Call MoveFiles()

    End Sub

我现在都被chuffed,因为iv也设法让它查找具有.success扩展名的所有文件。现在它处理所有文件而不是代码中指定的文件。

Module Module1    
Dim sourceDir As String = My.Settings.watchPath
Dim processedDir As String = My.Settings.processedPath

  Private loggerName As String = "EmailReleases"

Public Sub log(ex As Exception)
Console.WriteLine("Error: " & ex.ToString)
End Sub

Public Sub log(ByVal s As String)
Console.WriteLine(DateTime.Now.ToString & " [" & loggerName & "] " & s)
End Sub


Public Sub Main()    
Dim inputFiles As String() = Directory.GetFiles(sourceDir, "*.success")
log("Starting processing of .success files in '" & sourceDir & "' ... ")
If (inputFiles.Length > 0) Then
  Dim connection As New SqlConnection(My.Settings.connectionString)
  connection.Open()
  For Each fileName As String In inputFiles
    Dim sqlCommand As SqlCommand
    Dim fFile As New FileInfo(fileName)
    log(" Processing  " & fFile.Name)
    Dim betacell As String = fFile.Name.Substring(0, fFile.Name.Length - 8)

    'Update Status on Database with the use of the Betacell
    Dim updateTransaction As SqlTransaction = connection.BeginTransaction()
            Dim sqlQ As String = "UPDATE " & My.Settings.JobTb & " SET Status = '10'  WHERE JobNumber ='" & betacell & "'"
    sqlCommand = New SqlCommand(sqlQ, connection, updateTransaction)
    Dim result = sqlCommand.ExecuteNonQuery()

    'Email COnfirmation
    SendEmail(My.Settings.emailUsers, "EMAIL TEXT")
    If (result > 0) Then
      'Move the file
      fFile.MoveTo(processedDir & fFile.Name)
      updateTransaction.Commit() ' make sure to commit only in case moving the file is OK
    Else
      log("ERROR - Betacell '" & betacell & "' not found in database!")
      updateTransaction.Rollback()
    End If

1 个答案:

答案 0 :(得分:3)

不是轮询文件夹(即每隔n秒检查一次是否有新文件),而是让操作系统通知您该文件夹中的更改会更有效。您可以通过创建FileSystemWatcher来完成此操作。有an example on MSDN

但是,如果确实想要轮询文件夹,那么它实际上很简单 - 只需将以下代码包装在Timer中即可。请注意,我通常使用C#编写代码,如果语法不是100%,请道歉......

Imports System.IO

....

For Each file as String in Directory.GetFiles("C:\SomeFolder")
    DoSomethingWithFile (file)
Next