-3

I've started to wonder how do i manipulate files in a pc and the first and, i think, the most simple start should be moving all from one folder to another, though doing the research all the microsoft pages used examples with a specified file to move. So i've tried doing the implementation of the code (simple removing the specified file from directory), but it doesn't seem to work, can you guys give me an example of how to to use File.Move() or Directory.Move() functions to move ALL files in a folder? BTW i am kind of a beginner, so don't throw a massive code full of unknown stuff :P

ive tried a simple one: EDIT:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"C:\TESTmove\Location";
        string path2 = @"C:\TESTmove\Destination";

        if (Directory.Exists(path))
        {

                Directory.Move(path, path2);

        }


        Console.ReadKey ();
    }
}

OK, so now i dont have my destination folder created. It executes, moves files to destination folder and demolishes the location folder. How to fix that deleting?

  • 3
    Show us what you've tried, this really isn't an example requesting service, we'll help you fix your code, but we won't write it for you. The MSDN documentation is pretty clear (with examples even) on `File.Copy`, `File.Move` and `Directory.GetFiles` methods. – Ron Beyer Sep 24 '15 at 17:42
  • So what doesn't work? Can you make the effort of helping us help you? – async Sep 24 '15 at 17:48
  • 1
    OK, what do you think "Cannot create a file when that file already exists" means? – async Sep 24 '15 at 17:50
  • You will get an IOException if path2 already exists. Check the docs https://msdn.microsoft.com/en-us/library/system.io.directory.move(v=vs.110).aspx. – John Paul Sep 24 '15 at 17:52
  • One guy told me that it tries to create a folder named "destination", but it cant since it already exists – Mister White Sep 24 '15 at 17:52

3 Answers3

1

From the documentation:

(throws) IOException [...] when destDirName already exists.

Make sure the destination directory does not exist before using Move. If you want to check in code, you may want to make use of the Exists method.

Actually we've already had this here on SO.

Community
  • 1
  • 1
async
  • 1,537
  • 11
  • 28
  • OK so i got it :P, i declare the destination folder in my path, but i dont create it. So did that. Executed my program and it deleted my location folder.. I don;t need that – Mister White Sep 24 '15 at 18:11
  • No... You should check if path2 already existst. If it does, you don't want to move anything. If it doesn't, carry on with your move. Something like `if(! Directory.Exists(path2)) { Directory.Move(path, path2); }` – async Sep 24 '15 at 18:13
  • Why don't you check the documentation and see how the method really works and what to expect from it? "now i dont have my destination folder created. It executes, moves files to destination folder and demolishes the location folder" doesn't even make sense. – async Sep 24 '15 at 18:23
  • ok so this method automatically deletes the location folder when moving files. So apparently it isn't suitable for me. I would require a method that moves the insides of the folder and keeps it. – Mister White Sep 24 '15 at 18:42
0

This is a duplicate of Cannot create a file when that file already exists when using Directory.Move. The issue is that the directory specified by path2 already exists.

Community
  • 1
  • 1
John Paul
  • 827
  • 2
  • 6
  • 16
0

Per MSDN: https://msdn.microsoft.com/en-us/library/system.io.directory.move(v=vs.110).aspx

Directory.Move() will throw an IOException for lots of reasons, one of which being

destDirName already exists.

What you might want to do would be something along the lines of this:

public static void MoveFiles(string sourceDir, string destDir) 
{
    string[] files = Directory.GetFiles(sourceDir);

    foreach(string file in files)
    {
        string dest = file.Replace(sourceDir, destDir);

        if (!File.Exists(dest))
            File.Move(file, dest);          
    }
}

It's a method that accepts 2 string arguments; the source directory with all the files and the destination directory to move the files to.

It grabs an array containing the full path to each file in sourceDir.

Next it iterates over this array and switches out the sourceDir for the destDir in each string (leaving just the filename itself in tact) and moves it to the destination ONLY if it doesn't exist there already.

Note that I didn't include any checks to verify source / dest dir exist. You can look into how to do this on your own, and decide if you want the application to exit or to create those directories or what.

A good rule of thumb going forward is if you're getting an exception, check which line of your code specifically is throwing the exception and consult the MSDN page for those objects and methods to see what they do, what exceptions they throw, and why. This can help you determine why your code is blowing up, if using the debugger and stepping through it yourself isn't helping.

sab669
  • 3,984
  • 8
  • 38
  • 75