none
Run .bat every day at certain hour RRS feed

  • Question

  • Hello all, how can i make to run an .bat file every day at 05:00 am.
    This .bat will launch 3 instances of same .exe file. (same name in proccess file)

    After .bat start i want to check every minute if example.exe is still running, when is not running do things.

    Thanks

    Thursday, August 8, 2019 1:47 PM

Answers

  • Thanks for your help.

    I really prefer use vb for this operation. 

    Kind of expected that but writing this in code will entail a good deal of code along with possible permission issues.

    One option is to use the following C# library (which has a VB.NET example)

    https://github.com/dahall/taskscheduler

    From link above

    Imports Microsoft.Win32.TaskScheduler
    
    Module Module1
    
    	Sub Main()
    		SimpleExample()
    	End Sub
    
    	Sub ShowEditor()
    		Using ts As New TaskService()
    			' Create a new task
    			Const taskName As String = "Test"
    			Dim t As Task = ts.AddTask(taskName, New TimeTrigger(DateTime.Now + TimeSpan.FromHours(1)),
    				New ExecAction("notepad.exe", "c:\test.log", "C:\"))
    
    			' Edit task and re-register if user clicks OK
    			Dim regTask As Task = Nothing
    			Dim editorForm As New TaskEditDialog(t, True, True) With {.Title = "Edit Task"}
    			If editorForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
    				regTask = editorForm.Task
    			End If
    
    			' Remove the task we just created
    			ts.RootFolder.DeleteTask(taskName)
    		End Using
    	End Sub
    
    	Sub SimpleExample()
    		Using ts As New TaskService()
    			' Create a new task definition and assign properties
    			Const taskName As String = "Test"
    			Dim td As TaskDefinition = ts.NewTask
    			td.RegistrationInfo.Description = "Does something"
    
    			' Add a trigger that will fire every other week on Monday and Saturday and
    			' repeat every 10 minutes for the following 11 hours
    			Dim wt As New WeeklyTrigger()
    			wt.DaysOfWeek = DaysOfTheWeek.Monday Or DaysOfTheWeek.Saturday
    			wt.WeeksInterval = 2
    			wt.StartBoundary = Now
    			wt.Repetition.Duration = TimeSpan.FromHours(11)
    			wt.Repetition.Interval = TimeSpan.FromMinutes(10)
    			td.Triggers.Add(wt)
    
    			' Add an action (shorthand) that runs Notepad
    			td.Actions.Add(New ExecAction("notepad.exe", "C:\Test.log"))
    
    			' Register the task in the root folder
    			ts.RootFolder.RegisterTaskDefinition(taskName, td)
    
    			' Remove the task we just created
    			ts.RootFolder.DeleteTask(taskName)
    		End Using
    	End Sub
    
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by extream87 Tuesday, September 17, 2019 12:26 AM
    Thursday, August 8, 2019 3:52 PM
    Moderator
  • Hi,

    run an .bat file every day at 05:00 am

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim timer1 As New Timer With {.Enabled = True, .Interval = 1000}
            AddHandler timer1.Tick, Sub()
                                        Dim str As String = String.Format("{0:HH:mm:ss}", DateTime.Now)
    
                                        If str = "05:00:00" Then
                                            Dim info As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd.exe")
                                            info.FileName = "D:\my.bat"
                                            Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(info)
                                            proc.WaitForExit()
                                        End If
                                    End Sub
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by extream87 Tuesday, September 17, 2019 12:26 AM
    Friday, August 9, 2019 2:27 AM

All replies

  • Hello,

    Why not run the batch file using Windows scheduler. In regards to monitoring example.exe, this could be an executable also started by Windows scheduler and matter of fact you could run the batch file operation from an executable using Process class which accepts arguments.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, August 8, 2019 2:12 PM
    Moderator
  • Thanks for your help.

    I really prefer use vb for this operation. 

    Thursday, August 8, 2019 2:59 PM
  • Thanks for your help.

    I really prefer use vb for this operation. 

    Kind of expected that but writing this in code will entail a good deal of code along with possible permission issues.

    One option is to use the following C# library (which has a VB.NET example)

    https://github.com/dahall/taskscheduler

    From link above

    Imports Microsoft.Win32.TaskScheduler
    
    Module Module1
    
    	Sub Main()
    		SimpleExample()
    	End Sub
    
    	Sub ShowEditor()
    		Using ts As New TaskService()
    			' Create a new task
    			Const taskName As String = "Test"
    			Dim t As Task = ts.AddTask(taskName, New TimeTrigger(DateTime.Now + TimeSpan.FromHours(1)),
    				New ExecAction("notepad.exe", "c:\test.log", "C:\"))
    
    			' Edit task and re-register if user clicks OK
    			Dim regTask As Task = Nothing
    			Dim editorForm As New TaskEditDialog(t, True, True) With {.Title = "Edit Task"}
    			If editorForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
    				regTask = editorForm.Task
    			End If
    
    			' Remove the task we just created
    			ts.RootFolder.DeleteTask(taskName)
    		End Using
    	End Sub
    
    	Sub SimpleExample()
    		Using ts As New TaskService()
    			' Create a new task definition and assign properties
    			Const taskName As String = "Test"
    			Dim td As TaskDefinition = ts.NewTask
    			td.RegistrationInfo.Description = "Does something"
    
    			' Add a trigger that will fire every other week on Monday and Saturday and
    			' repeat every 10 minutes for the following 11 hours
    			Dim wt As New WeeklyTrigger()
    			wt.DaysOfWeek = DaysOfTheWeek.Monday Or DaysOfTheWeek.Saturday
    			wt.WeeksInterval = 2
    			wt.StartBoundary = Now
    			wt.Repetition.Duration = TimeSpan.FromHours(11)
    			wt.Repetition.Interval = TimeSpan.FromMinutes(10)
    			td.Triggers.Add(wt)
    
    			' Add an action (shorthand) that runs Notepad
    			td.Actions.Add(New ExecAction("notepad.exe", "C:\Test.log"))
    
    			' Register the task in the root folder
    			ts.RootFolder.RegisterTaskDefinition(taskName, td)
    
    			' Remove the task we just created
    			ts.RootFolder.DeleteTask(taskName)
    		End Using
    	End Sub
    
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by extream87 Tuesday, September 17, 2019 12:26 AM
    Thursday, August 8, 2019 3:52 PM
    Moderator
  • Hi,

    run an .bat file every day at 05:00 am

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim timer1 As New Timer With {.Enabled = True, .Interval = 1000}
            AddHandler timer1.Tick, Sub()
                                        Dim str As String = String.Format("{0:HH:mm:ss}", DateTime.Now)
    
                                        If str = "05:00:00" Then
                                            Dim info As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd.exe")
                                            info.FileName = "D:\my.bat"
                                            Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(info)
                                            proc.WaitForExit()
                                        End If
                                    End Sub
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by extream87 Tuesday, September 17, 2019 12:26 AM
    Friday, August 9, 2019 2:27 AM