A VB.NET program can be used to generate a report on a daily basis. But where should it store this report? Sometimes a file (named with the current day) is a good place.
By using some common String
and DateTime
functions, we can generate a file name for each day. Some surrounding characters can also be added.
To begin, we place some code in the Main
subroutine—this is run when the program is started. There are 3 parts to the codex ample.
String.Format
. We specify a format string
with year, month and day chars.Imports System.IO Module Program Sub Main() ' Part 1: generate file name from date. Dim name As String = String.Format("log-{0:yyyy-MM-dd}.txt", DateTime.Now) ' Part 2: get full path for file. Dim path As String = "C:\\programs\\" + name ' Part 3: write file with date in file name. File.WriteAllText(path, "Today is " + DateTime.Today.ToString()) Console.WriteLine("DONE") End Sub End ModuleDONE
Let us consider the output of the program in the year 2020. I tested that the file name is correct. And the file contents also matches the current day (when tested).
File: log-2020-06-19.txt Contents: Today is 6/19/2020 12:00:00 AM
Programming languages like VB.NET are often used for server-based applications, or for data analysis. Writing a daily file (from the results of a run) is a common requirement.