Hier einmal eine Lösung mit dem FileSystemWatcher: (Ich hatte noch ein bisschen Zeit übrig)
Eine Form mit btnStart (Button), btnStop (Button), tbImport (TextBox), tbExport (TextBox), llbImport (LinkLabel) und llbExport (LinkLabel)
dazu zwei Einstellungen ImportPfad (String) und ExportPfad (String). Anleitung
Imports System.IO
Imports System.Text
Public Class Form1
Private watcher As New FileSystemWatcher()
Public Sub New()
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
' Watch for changes in LastAccess and LastWrite times, and the renaming
' of files or directories.
watcher.NotifyFilter = (NotifyFilters.LastAccess Or _
NotifyFilters.LastWrite Or NotifyFilters.FileName Or _
NotifyFilters.DirectoryName)
' Only watch csv files.
watcher.Filter = "*.csv"
' Add event handlers.
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Created, AddressOf OnChanged
btnStop.Enabled = False
tbExport.ReadOnly = True
tbImport.ReadOnly = True
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles _
btnStart.Click
If Directory.Exists(tbImport.Text) And Directory.Exists(tbExport.Text) _
Then
watcher.Path = tbImport.Text
' Begin watching.
watcher.EnableRaisingEvents = True
btnStart.Enabled = False
btnStop.Enabled = True
Else
MessageBox.Show("Bitte beide Pfade angeben", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles _
btnStop.Click
' Stop watching.
watcher.EnableRaisingEvents = False
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
Private Sub FixCSV(ByVal InputFilePath As String, ExportFilePath As String)
Using tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = New _
Microsoft.VisualBasic.FileIO.TextFieldParser(InputFilePath, _
Encoding.Default)
tfp.SetDelimiters(";")
Using sw As StreamWriter = New StreamWriter(ExportFilePath, False, _
Encoding.Default)
Dim i As Integer = 0
Dim Zeile As String = ""
Do
Dim fields As String() = tfp.ReadFields
If fields Is Nothing Then Exit Do
For x As Integer = 0 To fields.Length - 1
If x = 30 Then
Zeile = String.Join(";"c, {Zeile, fields(x).Split( _
";"c)(i)})
Else
Zeile = String.Join(";"c, {Zeile, fields(x)})
End If
Next
sw.WriteLine(Zeile.Substring(1))
Zeile = ""
i += 1
Loop
End Using
End Using
End Sub
Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
' Specify what is done when a file is changed or created.
Dim exportFilePath As String = IO.Path.Combine(tbExport.Text, _
IO.Path.GetFileName(e.FullPath))
FixCSV(e.FullPath, exportFilePath)
End Sub
Private Sub llbImport_LinkClicked(sender As Object, e As _
LinkLabelLinkClickedEventArgs) Handles llbImport.LinkClicked
Dim dialog As New FolderBrowserDialog()
If tbImport.Text <> "" Then dialog.SelectedPath = tbImport.Text
If DialogResult.OK = dialog.ShowDialog() Then
tbImport.Text = dialog.SelectedPath
My.Settings.ImportPfad = tbImport.Text
My.Settings.Save()
End If
End Sub
Private Sub llbExport_LinkClicked(sender As Object, e As _
LinkLabelLinkClickedEventArgs) Handles llbExport.LinkClicked
Dim dialog As New FolderBrowserDialog()
If tbExport.Text <> "" Then dialog.SelectedPath = tbExport.Text
If DialogResult.OK = dialog.ShowDialog() Then
tbExport.Text = dialog.SelectedPath
My.Settings.ExportPfad = tbExport.Text
My.Settings.Save()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
tbExport.Text = My.Settings.ExportPfad
tbImport.Text = My.Settings.ImportPfad
End Sub
End Class |