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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:31:37+00:00 2026-06-15T15:31:37+00:00

Im trying to get my head around backgroundworker and the progressbar, so far i

  • 0

Im trying to get my head around backgroundworker and the progressbar, so far i have got it to work but not exactly how i want it to work. Basically i am sorting/renaming folders and copying them to a different location, this works and the code is self explanatory, the output folders generated are as expected. However for each folder i intend to search through i have to right click it to get the number of files and then in the code i have to set the progressBar1.Maximum to that value in order for it to show the coreect progression of the progress bar. How is it possible to get this to set the number of files automatically since it goes through each folder anyway? Some folders have thousands of files and others have millions. beyond this i want to add a label so that it displays the name of the file it is processing along with the progressbar updates.

namespace Data_Sorter
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
    }

    private void btnSort_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int totalFiles = 0;
        foreach (var file in Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories))
        {
            backgroundWorker1.ReportProgress(totalFiles);

            string fullFilename = file.ToString();

            string[] pathParts = fullFilename.Split('\\');
            string date = pathParts[6];

            string fileName = pathParts[7];

            string[] partName = fileName.Split('_');

            string point = partName[3];

            Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");

            if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                {
                    string destPath = (point + "\\" + date + "\\");
                    File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
            else
                {
                    MessageBox.Show("destination folder not found " + date + point);
                }

            totalFiles++;
        }
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Maximum = 6777; // set this value at the maximum number of files you want to sort //
        progressBar1.Value = e.ProgressPercentage;
    }
}
  • 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-06-15T15:31:39+00:00Added an answer on June 15, 2026 at 3:31 pm

    You can find out the file number simply reading the GetFiles length.

    You can pass the relative percentage using the expression: (i * 100) / totalFiles, in this way it’s not necessary to set the Maximum value for the progress.

    You can also report the filename to the progressbar passing it as the UserState in the progressChanged event.

    Try the code below:

       namespace Data_Sorter
        {
        public partial class Form1 : Form
        {
    
    
    public Form1()
        {
            InitializeComponent();
        }
    
        private void btnSelect_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            tbFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
        }
    
        private void btnSort_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int totalFiles = 0;
            string[] files = Directory.GetFiles(tbFilePath.Text, "*.txt", SearchOption.AllDirectories);
            totalFiles = files.Length;
            int i = 0;
            foreach (var file in files)
            {
    
                backgroundWorker1.ReportProgress((i * 100) / totalFiles, file);
                i++
                string fullFilename = file.ToString();
    
                string[] pathParts = fullFilename.Split('\\');
                string date = pathParts[6];
    
                string fileName = pathParts[7];
    
                string[] partName = fileName.Split('_');
    
                string point = partName[3];
    
                Directory.CreateDirectory("Data Sorted Logs\\" + point + "\\" + date + "\\");
    
                if (Directory.Exists(("Data Sorted Logs\\" + point + "\\" + date + "\\")))
                    {
                        string destPath = (point + "\\" + date + "\\");
                        File.Copy(fullFilename, "C:\\Documents and Settings\\PC\\Desktop\\Sorter\\Data Sorter\\bin\\Debug\\Data Sorted Logs\\" + destPath + fileName);                    }
                else
                    {
                        MessageBox.Show("destination folder not found " + date + point);
                    }
    
                totalFiles++;
            }
        }
    
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Done");
        }
    
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            progressBar1.Text = e.UserState.ToString();//or yourNewLabel.Text = e.UserState.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my head around why the following doesn't work. I have
trying to get my head around Feedzirra here. I have it all setup and
I'm trying to get my head around the covariance of Scala's collections. I have
I'm trying to get my head around the addressing of WCF services. We have
am trying to get my head around the following: Have a small program am
I'm trying to get my head around RewriteCond, and want to rewrite any requests
Im trying to get my head around MySQL. I have learnt a great deal
Im trying to get my head around javascript inheritance and this code doesnt work
Im trying to get my head around how to work with Linq2Sql in an
I'm trying to get my head around this behaviour: I have a ListView on

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.