Filename With Date Example
This page was last reviewed on Dec 21, 2022.
Dot Net Perls
Filename, date. 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.
Some details, file names. By using some common String and DateTime functions, we can generate a file name for each day. Some surrounding characters can also be added.
DateTime Format
An example. 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.
Part 1 We generate the file name by calling String.Format. We specify a format string with year, month and day chars.
Part 2 We build up the full path for the output file name. You can change this to point to a folder on your computer.
Part 3 We write a file using the full name and path we generated. The contents also contain the date.
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 Module
Notes, output. 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
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2024 Sam Allen.