Solved

VB.Net Loops, Simple Coding

Posted on 2007-04-03
2
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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