a big complicated program please help!!

seth-gr

Member
Joined
May 21, 2007
Messages
8
Location
Greece
Programming Experience
Beginner
Hello i have a problem with this code i am trying to build an application that monitors a folder for txt files when a txt file comes into the folder stop the monitoring take it make some changes and send it for printing via serial port then start again the monitoring and wait for file.but if the folder already has files when the program opens take them and send them to the printer.in addition because it is possible to have files with the same name in order to not to loose any file i want to move the files to a temp directory to rename them like 1.txt,2.txt etc.and then return them to the main file and prints them.....It's a little complicated...If i did n't explain a part and have questions just ask me I use vb.net 2002 with 2.0 framework.Any help or link will be precious thanks a lot

Here is the code or maybe a mess it can be compiled but is not working I beleive that i have found the concept to build this app but i dont have the knowledge to build it
for know this code if finds a file just gives me a msgbox I will go later to the changes and printing part

Thank you in advance


VB.NET:
Public watchfolder As FileSystemWatcher
Private m_SourceFilesDir As String
Private m_DestFilesDir As String
Private m_SourceFilesDir1 As String
Private m_DestFilesDir1 As String



Private Sub MoveFilestemp(ByVal Src As String, ByVal Dest As String)

m_SourceFilesDir = "E:\800\"
m_DestFilesDir = "E:\800\temp\"
Dim element As String
Dim ext As String = Text1.Text
Dim dir As String = Text2.Text


If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest &= Path.DirectorySeparatorChar
Dim Files() As String = Directory.GetFiles(dir, ext)
For Each element In Files
File.Move(element, Dest & Path.GetFileName(element))
Next element
End Sub

Private Sub MoveFilesmain(ByVal Src As String, ByVal Dest As String)

m_SourceFilesDir1 = "E:\800\temp\"
m_DestFilesDir1 = "E:\800\"
Dim element As String
Dim ext As String = Text1.Text
Dim dir As String = Text2.Text

If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest &= Path.DirectorySeparatorChar
Dim Files() As String = Directory.GetFiles(dir, ext)
For Each element In Files
File.Move(element, Dest & Path.GetFileName(element))
Next element
End Sub

Private Sub startmon()
watchfolder.EnableRaisingEvents = True
End Sub

Private Sub stopmon()
watchfolder.EnableRaisingEvents = False
End Sub

Private Sub createdirtemp()
If Directory.Exists("e:\800\temp\") = False Then

Directory.CreateDirectory("e:\800\temp\")

End If
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call createdirtemp()


End Sub

Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

If e.ChangeType = IO.WatcherChangeTypes.Created Then

Call stopmon()
MsgBox("there is a file")

Call MoveFilestemp(m_SourceFilesDir, m_DestFilesDir)
Call startmon()
Call MoveFilesmain(m_SourceFilesDir1, m_DestFilesDir1)
End If
Return

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Text1.Text = "" Then
MsgBox("enter extension")
Else

watchfolder = New System.IO.FileSystemWatcher()
watchfolder.Path = Text2.Text 'enter the path of the folder
watchfolder.Filter = Text1.Text 'enter the type / extension of the file
AddHandler watchfolder.Created, AddressOf logchange

Call startmon()

End If

End Sub

End Class



I noticed that if i try to run it and instead of msgbox to move the file to temp directory (without use startmon or stopmon)i have an error in this line

If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest &= Path.DirectorySeparatorChar

Object reference not set to an instance of an object.
 
lots of things wrong with the code.
You should be able to debug and step through the code to see where your error is. Hover over the variables while stepping through to see their values.

Note that MoveFilestemp & MoveFilesmain are virtually the same. There's no need for both of them.

A hint: when logchange is run, your private variables (m_SourceFilesDir etc) have no value.
 
Back
Top