Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 910059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:58:58+00:00 2026-05-15T16:58:58+00:00

I’m trying to loop through my foreach loops and spit out output to my

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:58:59+00:00Added an answer on May 15, 2026 at 4:58 pm

    I suspect you have a DataGridViewTextBoxColumn variable/field called ‘Path’ somewhere. Rename that variable and/or use the full name System.IO.Path.GetFileName() etc.

    As a more general remark, stop using the DatagridView as a data-structure. Do yourself a favor, define a class with the (column) properties you want, declare a BindingList<MyFileClass> and bind the DataGridView to that list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.