VB.Net Loops, Simple Coding


Couple of things i need to do:

I have a source location c:\
Within here i have files a.tif, b.tif, c.tif
I want to move them to a target location (which i know how to do) but i want them to a_1.tif, b_1.tif, c_1.tif
However, if a_1.tif exists already in the target i want to create a_2.tif etc. This loop i want to be endless
How would i code this

Also at the moment i am testing the code with a button click in the form. If i wanted to make this run automatically do i create an exe. Does this exe when run open the form as i want it to run in the background?

Finally i want to check my source folder every "x" seconds. How is this possible? I presume you can set the .exe to look in the source folder every "x" secs but i don't know how to do it.
Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
You don't necessarily need a Windows Service though.  You could just use a regular WinForms project but start from Sub Main so no Form is displayed.  Use the ApplicationContext() class to give your app a message pump.

Something like...

Module Module1

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

    Public Class MyContext
        Inherits ApplicationContext

        Private WithEvents tmr As New System.Windows.Forms.Timer

        Public Sub New()
            tmr.Interval = 15000 ' every 15 seconds
        End Sub

        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick

            ' do something in here...
            Debug.Print("Tick: " & DateTime.Now.ToString)

        End Sub

    End Class

End Module

To see if a file exists, use


You could loop until you get an unused name with something like:

        Dim intVersion As Integer = 1
        While System.IO.File.Exists("a_" & intVersion.ToString & ".tif")
            intVersion += 1
        End While

To have something run every x seconds, create a Timer control for your form and specify the length of time (in milliseconds, i.e., 5 seconds is entered as 5000) between when it should fire in its Interval property (be sure to set the Enabled property to True, as it's set to False by default). The program will need to be running continuously in the background. In the Timer Tick event handler, you put the code that should run when the appropriate time has elapsed.

If you want to create a program that runs only in background and doesn't display a form, you need to create a "Windows service." Describing that in detail is beyond the scope of what I can get into here. Information is available in the online help for Visual Studio, and I'm sure there's more at msdn.microsoft.com, though you might find it more productive to get a programming textbook that would walk you through such things in more detail.
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.