I have an app that recurses through a drive and outputs files that have “invalid” characters. Now that I have to put a UI to it, i’m a bit confused.
Prior to this being a WinForm app, the code was (mind you, this STARTED as a Console App):
class Program
{
public static void Main(string[] args)
{
//recurse through files. Let user press 'ok' to move onto next step
string[] files = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
Console.Write(file + "\r\n");
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
//End section
//Regex -- find invalid chars
string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
string replacement = " ";
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories);
List<string> filePath = new List<string>();
//clean out file -- remove the path name so file name only shows
foreach(string fileNames in fileDrive)
{
filePath.Add(fileNames);
}
using (StreamWriter sw = new StreamWriter(@"S:\bob.smith\File_Renames.txt"))
{
//Sanitize and remove invalid chars
foreach (string Files2 in filePath)
{
try
{
string filenameOnly = Path.GetFileName(Files2);
string pathOnly = Path.GetDirectoryName(Files2);
string sanitizedFilename = regEx.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFilename);
sw.Write(sanitized + "\r\n");
System.IO.File.Move(Files2, sanitized);
}
//error logging
catch(Exception ex)
{
StreamWriter sw2 = new StreamWriter(@"S:\bob.smith\Error_Log.txt");
sw2.Write("ERROR LOG");
sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n");
sw2.Flush();
sw2.Close();
}
}
}
}
Now that i have to implement a UI, i was hoping to populate a ListView with file names that contain invalid characters. I ONLY want to display those files that need to be renamed — not all files like it’s doing now. How do i do this and have it list these files in the ListView? Anybody have any syntax that might help?
1 Answer