File Organizer: My first C# project

FileOrgangizer

So I’ve finished up my first real C# WinForm project. It takes two paths, From and To, then either moves or copies all files from one to the other while organizing all the files into the new path by year and date.

The inspiration for the project came from my desire to take all the pictures that my wife and I have taken over the years and sort them all out by date into a nice folder structure that makes it easier to browse through. And yeah, there are better apps out there for stuff like this, but I’m learning here! 😛

using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace File_Organizer
{
    public partial class Form1 : Form
    {
        int numFiles;
        string[] files;
        string fromPath = "";
        string toPath = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonFrom_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                fromPath = folderBrowserDialog1.SelectedPath.ToString();
                textBoxFrom.Text = fromPath;
                GetNumberOfFiles();
            }
        }

        private void buttonTo_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                toPath = folderBrowserDialog1.SelectedPath.ToString();
                textBoxTo.Text = toPath;
            }
        }

        private void textBoxFrom_TextChanged(object sender, EventArgs e)
        {
            fromPath = textBoxFrom.Text;
        }
        
        private void textBoxTo_TextChanged(object sender, EventArgs e)
        {
            toPath = textBoxTo.Text;
        }

        private void textBoxFrom_Leave(object sender, EventArgs e)
        {
            GetNumberOfFiles();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            progressBar1.Value = 0;
            int i = 0;
            if (fromPath == "")
                MessageBox.Show("Please enter a From Path");
            else if (toPath == "")
                MessageBox.Show("Please enter a To Path");
            else if (numFiles == 0)
            {
                MessageBox.Show("No files to process");
                this.Cursor = Cursors.Default;
            }
            else
            {
                foreach (string file in files)
                {
                    FileInfo fileinfo = new FileInfo(file);
                    string newFolderPath = toPath + "\\" + fileinfo.LastWriteTime.Year + "\\" + fileinfo.LastWriteTime.Month;
                    string newFilePath = file.Replace(fromPath, newFolderPath);

                    if (Directory.Exists(newFolderPath) == false)
                    {
                        Directory.CreateDirectory(newFolderPath);
                    }

                    if (!copyCheck.Checked)
                    {
                        try
                        {
                            fileinfo.MoveTo(newFilePath);
                        }
                        catch (Exception)
                        {
                            //MessageBox.Show(ex.Message);
                        }
                    }

                    if (copyCheck.Checked)
                    {
                        try
                        {
                            fileinfo.CopyTo(newFilePath, true);
                        }
                        catch (Exception)
                        {
                            //MessageBox.Show(ex.Message);
                        }
                    }

                    i++;
                    progressBar1.Value = i;
                }

                this.Cursor = Cursors.Default;
            }
        }

        private void GetNumberOfFiles()
        {
            if (Directory.Exists(fromPath))
            {
                try
                {
                    files = Directory.GetFiles(fromPath, "*", SearchOption.AllDirectories);
                    numFiles = files.Count();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
            if (numFiles == 1) { labelOutput.Text = numFiles + " file found to organize."; }
            else { labelOutput.Text = numFiles + " files found to organize."; }

            if (numFiles > 0) { progressBar1.Maximum = numFiles; }
        }
    }
}
Advertisements
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s