Solved

VB.Net Loops, Simple Coding

Posted on 2007-04-03
2
215 Views
Last Modified: 2010-04-23
Hi,

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.
0
Comment
Question by:doctech
2 Comments
 
LVL 20

Expert Comment

by:ElrondCT
ID: 18843117
To see if a file exists, use

    System.IO.File.Exists("myfile")

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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 18844633
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
            tmr.Start()
        End Sub

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

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

            tmr.Start()
        End Sub

    End Class

End Module

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now