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 873705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:59:00+00:00 2026-05-15T10:59:00+00:00

I have an app that recurses through a drive and outputs files that have

  • 0

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 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-15T10:59:01+00:00Added an answer on May 15, 2026 at 10:59 am
    foreach(string fileNames in fileDrive)
    {
        if(regex.IsMatch(fileNames)) //check that it has invalid characters
        {
            filePath.Add(fileNames);
            yourListView.Items.Add(fileNames); //Add the string to your list view
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a business app that I have written, that effectively recurses through a
I have a web app that users upload files to. I want to authenticate
I have an app that plays radio stations, and I now want to integrate
I have an app that loops through a fixed width text file, reads each
I have an app that needs to open a new window (in the same
I have an app that executes commands on a Linux server via SSH just
I have a app that I'm deploying to a development server using Capistrano. I'd
I have an app that is failing on the install. The vendor says I
I have an App that will send authenticated emails using System.Net.Mail and System.Net.NetworkCredential my
I have an app that needs to handle very large strings between a SQL

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.