Exercise - Create files and directories
The File
and Directory
classes in .NET let you create new files and directories programmatically.
So far, you've created a robust command-line application in .NET that can read any folder structure to find files with a .json extension. Now, you need to read those files to summarize the data in them, then write the totals to a new file in a new directory called salesTotals.
In this exercise, you create the salesTotalDir directory and the totals.txt file where the sales totals are collated.
Create the SalesTotals directory
In the
Program.cs
file, remove theforeach
loop that iterates and writes each filename returned from theFindFiles
function to the Console output. This change results in thesalesFiles
variable going unused. However, we'll leave it in here for now because we'll use it again in a future lesson.In the
Program.cs
file, create a variable calledsalesTotalDir
, which holds the path to the salesTotalDir directory:var currentDirectory = Directory.GetCurrentDirectory(); var storesDirectory = Path.Combine(currentDirectory, "stores"); var salesTotalDir = Path.Combine(currentDirectory, "salesTotalDir"); var salesFiles = FindFiles(storesDirectory);
In the
Program.cs
file, add code to create the directory:var currentDirectory = Directory.GetCurrentDirectory(); var storesDirectory = Path.Combine(currentDirectory, "stores"); var salesTotalDir = Path.Combine(currentDirectory, "salesTotalDir"); Directory.CreateDirectory(salesTotalDir); // Add this line of code var salesFiles = FindFiles(storesDirectory);
Write the totals.txt file
In the
Program.cs
file, add the code to create an empty file called totals.txt inside the newly created salesTotalDir directory. Use an empty string for the file's contents for now:var currentDirectory = Directory.GetCurrentDirectory(); var storesDirectory = Path.Combine(currentDirectory, "stores"); var salesTotalDir = Path.Combine(currentDirectory, "salesTotalDir"); Directory.CreateDirectory(salesTotalDir); var salesFiles = FindFiles(storesDirectory); File.WriteAllText(Path.Combine(salesTotalDir, "totals.txt"), String.Empty);
Press Ctrl+S / Cmd+S to save the file.
Run the program by running the following code from the terminal prompt:
dotnet run
Select the Refresh icon in the Files explorer.
You're almost finished. The last step is to read the sales files, add up the totals, and write the grand total to the new totals.txt file. Next, you'll learn how to read and parse data inside files.
Got stuck?
If you got stuck during this exercise, here's the full code up to this point:
var currentDirectory = Directory.GetCurrentDirectory();
var storesDirectory = Path.Combine(currentDirectory, "stores");
var salesTotalDir = Path.Combine(currentDirectory, "salesTotalDir");
Directory.CreateDirectory(salesTotalDir);
var salesFiles = FindFiles(storesDirectory);
File.WriteAllText(Path.Combine(salesTotalDir, "totals.txt"), String.Empty);
IEnumerable<string> FindFiles(string folderName)
{
List<string> salesFiles = new List<string>();
var foundFiles = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);
foreach (var file in foundFiles)
{
var extension = Path.GetExtension(file);
if (extension == ".json")
{
salesFiles.Add(file);
}
}
return salesFiles;
}