Dailycode.info

Short solution for short problems

Directory copy(program attached to this post can be downloaded)

On request I created a small program that can copy one directory(including subdirectoies and files) to another directory. Nothing special, windows can do that. But The requirement was that only files created after a certain date needed to be copied.
It can be usefull for backup strategies or archiving.
Here a screenshot of this program.
 

 
Feel free to ask for the source code. Here a little snippet (This is a recursive function that loops over all files and directories. This functino is called in a seperate thread, so its not blocking the main thread and it can be stopped at all times.):
 
private void CopyDirStructure(string path, string searchdir)
{
    try
    {
        bool noOutput=false;
        if (GetOutputSize() > 2147400000)
        {
            SetOutput("\n!!!rest concatinated, to many files...!!!\n");
            noOutput=true;
        }
 
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo[] subDirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();
        
        int dept = 0;
 
        if (dir.FullName != searchdir)
        {
            if (files.Length > 0)
            {
                CreateDirAtDest(dir, noOutput);
            }
        }
        
        
        
            
        decimal dSize = 0;
        foreach (FileInfo fi in files)
        {
            bool bCopy = true;
            
            if (UseModifyDate())
            {
                if (fi.LastWriteTime < GetModifyDate())
                {
                    bCopy = false;
                }
            }
            if (bCopy)
            {
                if (CopyFileToDest(fi.FullName.ToString(), noOutput))
                {
                    dSize += Math.Round((decimal)fi.Length / 1024, 0, MidpointRounding.ToEven);
                    totalFileCount++;
                }
            }
        }
 
        
        
        totalSize += dSize;
       
        if (subDirs != null)
        {
            dept++;
            foreach (DirectoryInfo sd in subDirs)
            {
                CopyDirStructure(path + @"\\" + sd.Name, searchdir);
                totalDirCount++;
            }
        }
 
        SetDirCount(totalDirCount);
        SetFileCount(totalFileCount);
        SetSize(totalSize);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message.ToString());
    }
}
The controls are updated after each file, because the copy is running in a seperate thread,
be sure that you invoke the controls before trying to update them:
 
public void SetFileCount(int c)
{
    if (this.lbltotal.InvokeRequired)
    {
        this.lbltotal.BeginInvoke(
            new MethodInvoker(
            delegate() { SetFileCount(c); }));
    }
    else
    {
        this.lbltotal.Text = c.ToString() + " files";
    }
}

Download the program here:

CopyFiles.zip (62.17 kb)


Directory structure generator version 1.1

I made some adjustements to the directory structure generator which have lead to new version. The major change is that I do the generation in a seperate thread. Now I can update the info during the process, the main thread is not blocked. It is also possible to stop the porcess whenever you like.

I added some extra features which can be nice. E.g. sorting on date, show modifydate in result, ...

Here is a screenshots of the latest version:

The exe file and source files can be requested.

You can read here how it all started.

 


Directory structure generator

Many people have a certain logic in the way they build up their files system, and so they should. Sometimes you want to view this structure or export it because you want to use it for some kind of purpose. Then you will have a hard time in windows getting the structure in an organized way to a file or excel sheet.

 

For this reason I created a tool that helps you export a directory structure. It started out just exporting the structure if the directories and files, but now you can use checkboxes to add more information to the export.

You can:

  • Choose to see only file or dir names or the complete path.
  • Choose the depth of the search. (0 is only the root directory) 
  • Choose to show the size of the files.
  • Then for extra information on the top right you can see the number of files and directories that are shown. 

There is a new version available for download. You can find more info here in part 2. 

You can download this project latest version as an executable and/or the source files to get the function you need out of it.

 

 

 

 


Exporting a directory structure

This program is now updated and available as download on this blog, more on this here)
I was looking for a way to export the directory structure of a given directory. I found a simple recursive way to do this. The output is a richhtextbox, where the users can copy paste the structure. This is how it looks (directories are marked in red, files in black):
 
 
I added some features like, show files in structure and to options to show or hide the full file or directory names.
Next the piece of code that does the trick. No fancy stuff, just a simple recursive method that fills a richt text box. First the onlcick event behond the generate button, then the method that generates the directory structure.
 
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            rtxtOutput.Text = "";
            if (!String.IsNullOrEmpty(brwSource.SelectedPath))
            {
                DirectoryInfo searchdir = new DirectoryInfo(brwSource.SelectedPath);
                GetDirStructure(brwSource.SelectedPath, searchdir.FullName);
            }
        }
  
        public void GetDirStructure(string path, string searchdir)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                
                DirectoryInfo[] subDirs = dir.GetDirectories();
                FileInfo[] files = dir.GetFiles();
                
                int dept = 0;
 
                if (dir.FullName != searchdir)
                {
                    DirectoryInfo parent = dir;
                    while (parent.FullName != searchdir)
                    {
                        parent = parent.Parent;
                        dept++;
                    }
                }
                string deptstring ="  ";
                for (int i = 0; i < dept; i++)
                {
                    deptstring = deptstring + "  ";
                }
                rtxtOutput.SelectionColor = Color.Red;
                if (cbxFull.Checked)
                {
                    rtxtOutput.SelectedText = String.Format("{1}{0}\n", dir.FullName, deptstring);
                }
                else
                {
                    rtxtOutput.SelectedText = String.Format("{1}{0}\n", dir.Name, deptstring);
                }
                rtxtOutput.SelectionColor = Color.Black;
                deptstring = deptstring + "  ";
                if (cbxFiles.Checked)
                {
                    foreach (FileInfo fi in files)
                    {
                        if (cbxFullFile.Checked)
                        {
                            rtxtOutput.SelectedText = String.Format("{1}{0}\n", fi.FullName.ToString(), deptstring);
                        }
                        else
                        {
                            rtxtOutput.SelectedText = String.Format("{1}{0}\n", fi.Name, deptstring);
                        }
                    }
                }
                if (subDirs != null)
                {
                    foreach (DirectoryInfo sd in subDirs)
                    {
                        dept++;
                        GetDirStructure(path + @"\\" + sd.Name, searchdir);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
      }
        }
  
 Here you can download the program: