0

I'm creating a little desktop application that will allow me to specify 3 fields(with example data):

Folder - D:\Programming\Storage for testing\FileMover\Folder_1

Destination Folder - D:\Programming\Storage for testing\FileMover\Folder_2

File Type - .txt

So I want to move all of the files with the file type .txt from the Root Folder to the Destination Folder.I read using 'Directory.Move()' requires you to move the file to another file within the specified directory. So I wrote a little function that builds up the file path for the new file but keep the name so:

Root Folder File : "\Folder_1\Test.txt"

Run Create new filesDestination Folder : "\Folder_2\Test.txt" This bit works but when I attempt Directory.Move() it says the file in the destination folder is in use. How can I go about making sure it is not in use before the move?

Some Variables to help understand:

FileType = "txt" RootDirectory = D:\Programming\Storage for testing\FileMover\Folder_1

destinationFolder = D:\Programming\Storage for testing\FileMover\Folder_2

newFileDestination = D:\Programming\Storage for testing\FileMover\Folder_2\Test1.txt

filesToCopy = every file within Folder_1 with the file type of FileType

public override string[] CreateEmptyFiles()
{
    var fileTypeFormat = "*." + FileType;
    var filesToCopy = Directory.GetFiles(RootDirectory, fileTypeFormat);

    //file is the file path of the root directory including the file
    foreach (var file in filesToCopy)
    {  
        //Split the file hierarchy into a string array
        var directoryHierarchy = file.Split('\\');  

        //Get the file name 
        var fileName = directoryHierarchy[directoryHierarchy.Length - 1];  

        //Create the new file path
        var newFileDestination = destinationFolder + "\\" + fileName;
        using (FileStream fileStream = new FileStream(newFileDestination, FileMode.Create))
        {  
            //Create the new file
            File.Create(newFileDestination);
        }
        //Exception thrown here
        Directory.Move(newFileDestination, file);
    }

    return new string[10];
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • What happens if you remove/comment out the using block with `File.Create` in it? – T2PS Sep 05 '18 at 09:18
  • 2
    You open a file stream with `FileStream`. Then, you are calling `File.Create` which actually does pretty the same - it creates a file and returns a `FileStream`, and obviously it fails, because file is already used. Just remove one of them. – Yeldar Kurmangaliyev Sep 05 '18 at 09:18
  • [Is there a way to check if a file is in use?](https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – TheGeneral Sep 05 '18 at 09:19
  • @YeldarKurmangaliyev I've removed the File.Create but I'm now getting 'File Already Exists' error.. even though Directory.Move() needs a matching file to move to? – Ceri Westcott Sep 05 '18 at 09:23
  • @YeldarKurmangaliyev 'System.IO.IOException: 'Cannot create a file when that file already exists.' – Ceri Westcott Sep 05 '18 at 09:23
  • I suggest you don't move folders and files like this. Many things can go wrong and you will end up with a mess. Consider copy , log, delete approach. – NoChance Sep 05 '18 at 09:24
  • Solution is fixed but not sure whether it is the correct way. I will post as an answer. – Ceri Westcott Sep 05 '18 at 09:28
  • If you want to just move files from one directory to another, then you don't need to create files at all. – Yeldar Kurmangaliyev Sep 05 '18 at 09:30

2 Answers2

0

As the comments say, I had a File.Create within a FileStream Creating the same file so thats why I got the access issue.

My solution using NoChances tips(Will put logging/Deletion in later today).

public override string[] CreateEmptyFiles()
{
    var fileTypeFormat = "*." + FileType;
    var filesToCopy = Directory.GetFiles(RootDirectory, fileTypeFormat);
    foreach (var file in filesToCopy)
    {
        var directoryHierarchy = file.Split('\\');
        var fileName = directoryHierarchy[directoryHierarchy.Length - 1];
        var newFileDestination = destinationFolder + "\\" + fileName;
        //Copy File over
        File.Copy(file,newFileDestination);
        File.Delete(file);
    }
    return new string[10];
}
0

Why not File.Move(...) instead of FileStream?

public override string[] CreateEmptyFiles() {
  // Enumerate required files...
  foreach (var file in Directory.EnumerateFiles(RootDirectory, "*." + FileType)) {
    var newFileDestination = Path.Combine(destinationFolder, Path.GetFileName(file));

    // ...and move/copy them
    // File.Copy if you want to copy
    File.Move(file, newFileDestination);
  } 

  ...
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215