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
Solved

VB.Net Loops, Simple Coding

Posted on 2007-04-03
2
219 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to limit User Input 2 27
How to duplicate form in Visual Studio 2015 2 60
SSIS Start 2 21
Retrieve Active Directory Groups a User belongs to in VB.NET 3 19
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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