Automate tasks using VB.net

I have this code to automate folder backups and it works well. However, I want to cut out the caption, forcing her to back up automatically 12PM every day. Ant clues on how to do this will be seriously appreciated.

Imports System.IO
Imports System.IO.Compression
Public Class Form1

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

    Dim dstr As String
    Dim mstr As String
    Dim ystr As String
    Dim folstr As String
    Dim dsumstr As String

    dstr = DateTime.Today.ToString("dd")
    mstr = DateTime.Today.ToString("MM")
    ystr = DateTime.Today.ToString("yyyy")
    dsumstr = ystr & "-" & mstr & "-" & dstr
    folstr = "Y:\server1\Fileserver-" & dsumstr
    Try
        My.Computer.FileSystem.CreateDirectory(folstr)

        My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
        My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")



        Label1.Text = "Back up DATE   " & dsumstr & "  Complete"
    Catch ex As Exception
        Label1.Text = (ex.Message)
    End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
End Sub
End Class
+3
source share
2 answers

For our Windows servers, we use Task Scheduler. You can read it here .

enter image description here

+2
source

Add a timer - as follows:

Dim WithEvents Timer1 As Timer

Set up your variables (or read them anywhere).

Dim runtimestring As String = "12:00 PM"
Dim nextruntime As Date

At startup, set the interval for the following runtime and start the timer:

Sub New()
    'set up time interval
    If Now.TimeOfDay > Date.Parse(runtimestring).TimeOfDay Then
        'set nextruntime to tomorrow
        nextruntime = Date.Parse(runtimestring).AddDays(1)
    Else
        'set nextruntime to today
        nextruntime = Date.Parse(runtimestring)
    End If
    Timer1.Interval = nextruntime.Subtract(Now).TotalMilliseconds
    Timer1.Start()
End Sub

Tick , /, , .

Sub Timer1_Tick() Handles Timer1.Tick
    Timer1.Stop()
    'Add your functions and sub here.....


    'set nextruntime to tomorrow
    nextruntime = Date.Parse(runtimestring).AddDays(1)
    Timer1.Interval = nextruntime.Subtract(Now).TotalMilliseconds
    Timer1.Start()
End Sub
0

All Articles