Pergunta

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 ?

Foi útil?

Solução

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\";

Outras dicas

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.

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.)

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));

        }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top