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

-1

I'm having a hard time creating a folder within another folder and moving the content into that new folder. The rule is as follows:

  • I unzipped a .zip file into this directory: C:\Teste_Zip\Destino_Teste\Temp/> . Well if the web folder exists, then my business starts there.

  • Within the web folder, I should create two folders: FarmInterna and FarmExterna . Everything you have inside the web folder must be moved to the FarmInterna folder and the new FarmExterna folder will be copied to some files, based on another rule.

  • Well, the whole point is that when creating the new folder, I can not move the files and the folder is deleted and I do not know why.

    See 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 FarmInterna .

    By improving the question, the FarInterna folder must be inside the web folder and the folders and files that should be moved to this folder are only the folders and files that are inside the web folder, except the FarmInterna , it's obvious.

    I have refactored my code and when it arrives in foreach, when it gives a moveTo() , it says that it was not possible to find part of the way. That is, it enters with the correct path, but since the folder FarmInterna is empty, it must be this, 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);
                    }
                }    
            }
    
        
    asked by anonymous 14.03.2016 / 17:13

    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]);
                }
            }
    
        }
    
        
    04.11.2016 / 13:30