Copy content from a folder with all subfolders and files into a new one

Asked

Viewed 1,802 times

-1

I’m having a hard time creating a folder inside another one and moving the content into that new folder. The rule is:

  1. I unzip a file .zip inside that directory:C:\Teste_Zip\Destino_Teste\Temp/>. Well if there is a folder web, then my business starts.

  2. Inside the folder web, I must create two folders: FarmInterna and FarmExterna. Everything inside the briefcase web, must be moved to the folder FarmInterna and the new folder FarmExterna some files will be copied, based on another rule.

Well, the whole point is that when creating the new folder, I can’t move the files and the folder gets deleted and I don’t know why.

Look at my code:

private void CriaPastaFarmInterna()
{
    string path_trabalho = ConfigurationManager.AppSettings["Target_Dir_Temp"];
    string novo_path = path_trabalho + "\\FarmInterna";

    DirectoryInfo dirInfo = new DirectoryInfo(novo_path);

    if (dirInfo.Exists == false)
        Directory.CreateDirectory(novo_path);

        List<String> myFiles = Directory
                               .GetFiles(path_trabalho, "*.*", SearchOption.AllDirectories).ToList();

        foreach (string file in myFiles)
        {
            FileInfo mFile = new FileInfo(file);
                // to remove name collusion
            if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
            mFile.MoveTo(dirInfo + "\\" + mFile.Name);
        }            
    }

The need to create and move to the FarmInterna.

Improving the question, the folder FarInterna must be inside the web folder and the folders and files that must be moved to that folder, are only the folders and files that are inside the web folder, except the folder FarmInterna, it is obvious.

I redid my code and when it comes to foreach, it gives a moveTo(), He says he couldn’t find part of the way. That is, it enters with the correct path, but since the internal Farminternal folder is empty, that must be it, there is no folder. Below the new code.

private void CriaPastaFarmInterna()
{
    string path_trabalho = ConfigurationManager.AppSettings["Target_Dir_Temp"] + @"\web";
    string novo_path = path_trabalho + @"\FarmInterna";

    DirectoryInfo dirInfo = new DirectoryInfo(novo_path);

    if (dirInfo.Exists == false)
        Directory.CreateDirectory(novo_path);

        List<String> myFiles = Directory.GetFiles(path_trabalho, "*.*", SearchOption.AllDirectories).ToList();

        List<String> myDirectories = Directory.GetDirectories(path_trabalho).ToList();

        var diretorios = myDirectories.Where(d => !d.Contains("FarmInterna"));

        foreach (var di in diretorios)
        { 
            foreach (string file in myFiles)
            {
                FileInfo mFile = new FileInfo(file);
                string newFile = novo_path + (file.Replace(path_trabalho, ""));
                    //string newFile = novo_path + "\\" + (file.Replace(path_trabalho, ""));

                if (new FileInfo(newFile).Exists == false)
                    mFile.MoveTo(newFile);
                }
            }    
        }
  • There is nothing in your code that would cause the 'Farminterna' folder to be deleted. However, there are some inconsistencies in your code: 1. The method GetFiles lists only files and not directories. To list everything from the folder, you would also need to use the method GetDirectories. 2. When listing directories also, your folder FarmInterna would enter the list of what should be copied - you should add a check so that it is not moved as well.

  • @Marcusvinicius, my question is as follows. The Farminterna folder should be inside the web folder and when I do this, it gives me the message that is already being used and does nothing else.

1 answer

-1

  public void copiaDiretorio(string input,string output)
    {


        if (!Directory.Exists(output)) {
            Directory.CreateDirectory(output);
        }

        if (!string.IsNullOrEmpty(input))
        {
            string[] arquivos = Directory.GetFiles(input);
            foreach (string arquivo in arquivos)
            {
                File.Copy(input + @"\" + Path.GetFileName(arquivo), output + @"\" + Path.GetFileName(arquivo));
            }
            string[] diretorios = Directory.GetDirectories(input);
            foreach (string diretorio in diretorios)
            {
                copiaDiretorio(diretorio, output + @"\" + diretorio.Split(Convert.ToChar(@"\"))[diretorio.Split(Convert.ToChar(@"\")).Length - 1]);
            }
        }

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.