0

I am writing a large number of files (80,000 to be exact) that are on my hard drive and copying them to my flash drive. Things start ok, but at the 29,648th file, I get an IOException stating The directory or file cannot be created.

I used to different ways of directory copying that I've found via the interwebs:

https://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

Copy the entire contents of a directory in C#

And they both ended with the same result.

Any ideas on why it fails exactly there? The flash drive has enough space, and I know the file is not duplicate, since I am starting with a blank flash drive.

class Program
    {
        static void Main(string[] args)
        {            
            DirectoryCopy(Directory.GetCurrentDirectory(), "F://", true);
        }

        static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            // If the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
}
Community
  • 1
  • 1
MikeB
  • 2,402
  • 1
  • 15
  • 24
  • 1
    Please paste the code you are using. Apart from that , did you try to copy it manually( CTRL-C , CTRL-V) and see if it's getting copied ? – Jagannath Jan 23 '15 at 04:21
  • What format is the flash drive, i.e. FAT32, NTFS, ...? Are all of the files being copied to a single directory? Are you using long file names? Different formats have limits on the number of files allowed per directory and the limits may vary depending on file name lengths. – HABO Jan 23 '15 at 04:23
  • FAT32. Files names are huge_dummy_file{1}.csv...huge_dummy_file{80000}.csv – MikeB Jan 23 '15 at 04:38
  • I would avoid the weird symbols in your filenames, especially for FAT32. However I doubt that's your problem, the flash drive or your USB hub is most likely failing (hardware issue). – Blindy Jan 23 '15 at 04:42
  • 1
    FAT32 supports up to 65534 files per directory. As I understand it, using long file names (LFNs) causes the file system to store both an 8.3 name and the LFN in the directory. The longer the LFNs the fewer will fit. And LFNs are stored using 16-bit characters so they take twice the space. I haven't found a definitive reference that explains the limit, but [Wikipedia](https://en.wikipedia.org/wiki/File_Allocation_Table) is a start. – HABO Jan 23 '15 at 04:42
  • This is very interesting. A friend had the same error at the 16383 mark, which is 65,534/4 – MikeB Jan 23 '15 at 04:52
  • For reference... the FAT32 spec: https://msdn.microsoft.com/en-us/windows/hardware/gg463080.aspx – deepee1 Feb 11 '15 at 16:23

0 Answers0