Moving files from one folder to another C#
Asked Answered
S

4

40

Guys I am trying to move all files ending with _DONE into another folder.

I tried

//take all files of main folder to folder model_RCCMrecTransfered 
string rootFolderPath = @"F:/model_RCCMREC/";
string destinationPath = @"F:/model_RCCMrecTransfered/";
string filesToDelete = @"*_DONE.wav";   // Only delete WAV files ending by "_DONE" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach (string file in fileList)
{
    string fileToMove = rootFolderPath + file;
    string moveTo = destinationPath + file;
    //moving file
    File.Move(fileToMove, moveTo);

But on executing these codes i get an error saying.

The given path's format is not supported.

Where did I go wrong ?

Sphere answered 31/10, 2013 at 6:5 Comment(0)
P
35

Your slashes are in the wrong direction. On windows you should use back slashes. E.g.

string rootFolderPath = @"F:\model_RCCMREC\";
string destinationPath = @"F:\model_RCCMrecTransfered\";
Purify answered 31/10, 2013 at 6:6 Comment(2)
Do you mean, "On windows you should not use forward-slashes"?Loveinidleness
Path.Combine can help with platform-dependent path delimiters, although there are some caveats; see here: learn.microsoft.com/en-us/dotnet/api/system.io.path.combineUnprovided
Y
29

I made it this way:

if (Directory.Exists(directoryPath))
{
    foreach (var file in new DirectoryInfo(directoryPath).GetFiles())
    {
        file.MoveTo($@"{newDirectoryPath}\{file.Name}");
    }
}

file is a type of FileInfo class. It already has a Method called MoveTo() that takes a destination path.

Yonit answered 25/2, 2018 at 11:58 Comment(2)
I just implemented this in a project I'm working on. Thanks for the solution! :DHockett
This is like the only solution that I could get working. Thanks!!!Vidal
M
10

The array of file names returned from System.IO.Directory.GetFiles() includes their full path. (See http://msdn.microsoft.com/en-us/library/07wt70x2.aspx) This means that appending the source and destination directories to the file value isn't going to be what you expect. You'll end up with values like F:\model_RCCMREC\F:\model_RCCMREC\something_DONE.wav in fileToMove. If you set a breakpoint on the File.Move() line, you could look at the values you are passing, which can help debug a situation like this.

Briefly, you'll need to determine the relative path from rootFolderPath to each file in order to determine the proper destination path. Take a look at the System.IO.Path class (http://msdn.microsoft.com/en-us/library/system.io.path.aspx) for methods that will help. (In particular, you should consider Path.Combine() rather than + for building paths.)

Malachi answered 31/10, 2013 at 6:17 Comment(0)
A
1

Please try the below function. This works fine.

Function:

public static void DirectoryCopy(string strSource, string Copy_dest)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(strSource);

        DirectoryInfo[] directories = dirInfo.GetDirectories();

        FileInfo[] files = dirInfo.GetFiles();

        foreach (DirectoryInfo tempdir in directories)
        {
            Console.WriteLine(strSource + "/" +tempdir);

            Directory.CreateDirectory(Copy_dest + "/" + tempdir.Name);// creating the Directory   

            var ext = System.IO.Path.GetExtension(tempdir.Name);

            if (System.IO.Path.HasExtension(ext))
            {
                foreach (FileInfo tempfile in files)
                {
                    tempfile.CopyTo(Path.Combine(strSource + "/" + tempfile.Name, Copy_dest + "/" + tempfile.Name));

                }
            }
            DirectoryCopy(strSource + "/" + tempdir.Name, Copy_dest + "/" + tempdir.Name);

        }

        FileInfo[] files1 = dirInfo.GetFiles();

        foreach (FileInfo tempfile in files1)
        {
            tempfile.CopyTo(Path.Combine(Copy_dest, tempfile.Name));

        }
}
Adrell answered 10/1, 2017 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.