make c# code that will scan C:\AI\fish-speech\results for the most recent folder in that…
- User
make c# code that will scan C:\AI\fish-speech\results for the most recent folder in that directory then return only the folders name.
- Assistant
Certainly! Below is a C# code snippet that scans the directory
C:\AI\fish-speech\resultsfor the most recent folder based on the last write time, and returns only that folder’s name (not the full path):csharpusing System; using System.IO; using System.Linq; class Program { static void Main() { string parentDir = @"C:\AI\fish-speech\results"; if (!Directory.Exists(parentDir)) { Console.WriteLine("Directory does not exist."); return; } var mostRecentFolder = new DirectoryInfo(parentDir) .GetDirectories() .OrderByDescending(d => d.LastWriteTime) .FirstOrDefault(); if (mostRecentFolder != null) { Console.WriteLine(mostRecentFolder.Name); } else { Console.WriteLine("No folders found."); } } } - User
make it a void or whatever
- Assistant
Sure! Here's the code encapsulated into a
voidmethod that prints the most recent folder name, and a simpleMainmethod calling it:csharpusing System; using System.IO; using System.Linq; class Program { static void Main() { PrintMostRecentFolderName(@"C:\AI\fish-speech\results"); } static void PrintMostRecentFolderName(string parentDir) { if (!Directory.Exists(parentDir)) { Console.WriteLine("Directory does not exist."); return; } var mostRecentFolder = new DirectoryInfo(parentDir) .GetDirectories() .OrderByDescending(d => d.LastWriteTime) .FirstOrDefault(); if (mostRecentFolder != null) { Console.WriteLine(mostRecentFolder.Name); } else { Console.WriteLine("No folders found."); } } }