I'm trying to loop through my foreach loops and spit out output to my datagridview.
The datagridview has 3 defined columns: Path, Old File Name, New File Name
However, I keep hitting errors.
First, here is the code:
public partial class SanitizeFileNames : Form
{
public SanitizeFileNames()
{
InitializeComponent();
}
public void Sanitizer(List<string> paths)
{
string regPattern = (@"[~#&!%+{}]+");
string replacement = "";
Regex regExPattern = new Regex(regPattern);
List<string> existingNames = new List<string>();
StreamWriter errors = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Errors.txt", true);
StreamWriter resultsofRename = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Results of File Rename.txt", true);
dataGridView1.Rows.Clear();
var filesCount = new Dictionary<string, int>();
try
{
foreach (string files2 in paths)
{
string filenameOnly = Path.GetFileName(files2);
string pathOnly = Path.GetDirectoryName(files2);
string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFileName);
if (!System.IO.File.Exists(sanitized))
{
System.IO.File.Move(files2, sanitized);
DataGridViewRow dgr2 = new DataGridViewRow();
dgr2.Cells[0].Value = pathOnly;
dgr2.Cells[1].Value = filenameOnly;
dgr2.Cells[2].Value = sanitized;
resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + filenameOnly + "\r\n" + "New File Name: " + sanitized + "\r\n" + "\r\n");
}
else
{
if (filesCount.ContainsKey(sanitized))
{
filesCount[sanitized]++;
}
else
{
filesCount.Add(sanitized, 1);
}
string newFileName = String.Format("{0}{1}{2}",
Path.GetFileNameWithoutExtension(sanitized),
filesCount[sanitized].ToString(),
Path.GetExtension(sanitized));
string newFilePath = Path.Combine(Path.GetDirectoryName(sanitized), newFileName);
System.IO.File.Move(files2, newFilePath);
DataGridViewRow dgr2 = new DataGridViewRow();
dgr2.Cells[0].Value = pathOnly;
dgr2.Cells[1].Value = filenameOnly;
dgr2.Cells[2].Value = newFileName;
resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + Path.GetFileNameWithoutExtension(files2) + "\r\n" + "New File Name: " + newFileName + "\r\n" + "\r\n");
}
}
}
catch (Exception e)
{
errors.Write(e);
}
}
private void SanitizeFileNames_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
First off, I'm new to C# so I'm not even 100% sure this is correct. What this code does is take paths that are passed in from another method and "clean" the file names by removing invalid chars (as defined in my regex pattern). If the file name already exists, append the filename with a number that increases incrementally (i.e. ~test.txt and %test.txt would both be renamed to test.txt. with my code, ~test.txt becomes test.txt and %test.txt becomes test1.txt). I'm trying basically to append this data to my datagridview (should the file go to the else if(filesCount.ContainsKey(sanitized) loop).
I get errors with Path.GetFileNameWithoutExtension(), Path.GetExtension(), Path.Combine(), Path.GetDIrectoryName(). The error is as follows:
'System.Windows.Forms.DataGridViewTextBoxColumn' does not contain a definition for 'GetFileName' and no extension method 'GetFileName' accepting a first argument of type 'System.Windows.Forms.DataGridViewTextBoxColumn' could be found (are you missing a using directive or an assembly reference?)
I have no idea what this means or how to fix this. Can someone help?