Search does not recognize .zip file as a valid file inside a folder

4

I did this routine to delete empty folders, with no files inside.

foreach(var folder in folder_new)
        {
            if (Directory.Exists(folder))
            {
                if (Directory.GetFiles(folder, "*", SearchOption.AllDirectories).Length == 0)
                    {
                       Directory.Delete(folder, true);
                    }
             }
          }

So far beauty, except a problem. When I have a folder with only a .zip file inside, it deletes the folder, because it thinks the folder is empty. How do I outline this?

Ex: I have this folder tree:

web\ws\tiss\v3
string[] files_new = Directory.GetFiles(path_files, "*", SearchOption.AllDirectories);
string[] folder_new = Directory.GetDirectories(path_files, "*", SearchOption.AllDirectories);

And inside the 00 folder, I have the file TISS.zip . The folder ws and all its contents (subfolders) is deleted.

private void processaDiretorio(string inicio)
{
   foreach(var diretorio in Directory.GetDirectories(inicio))
   {
       processaDiretorio(diretorio);
       if (Directory.GetFiles(diretorio).Length == 0 &&
           Directory.GetDirectories(diretorio).Length == 0)
       {
           Directory.Delete(diretorio, false);
        }
   }
}

I have done this function and continue to delete folders and subfolder, if in the last there is only one file .zip .

private void processaDiretorio(string inicio)
        {

            DirectoryInfo di = new DirectoryInfo(inicio);

            foreach (var fi in di.GetDirectories())
            {
                processaDiretorio(fi.FullName);

                if (fi.GetFiles().Length == 0 && fi.GetDirectories().Length == 0)
                {
                    fi.Delete();
                }
            }
}

I changed the method to this one, using DirectoryInfo and even then I can not get .zip in folders.

foreach(var file in new DirectoryInfo(path_files).GetFiles())
{ 
    string s = file.Name;
}

But using this approach ( DirectoryInfo ), I can get .zip files inside.

foreach(var folder in folder_new)
        {
            if (Directory.Exists(folder))
            {
                if (Directory.GetFiles(folder, "*", SearchOption.AllDirectories).Length == 0)
                    {
                       Directory.Delete(folder, true);
                    }
             }
          }
    
asked by anonymous 07.03.2016 / 13:44

1 answer

1

I do not know much about C #, but in the documentation the Directory.GetDirectories had no parameters, however the parameters you mentioned directed to the documentation of DirectoryInfo , try doing this:

private void processaDiretorio(string inicio)
{
    DirectoryInfo di = new DirectoryInfo(inicio);

    foreach (FileInfo fi in di.GetDirectories())
    {
         processaDiretorio(fi.FullName);

         if (fi.GetFiles().Length == 0 && fi.GetDirectories().Length == 0) {
               fi.Delete();
         }
    }
}

See a simple test, create a Console application and paste the following content:

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            processaDiretorio("C:\test\");
            Console.ReadLine();
        }

        private static void processaDiretorio(string inicio)
        {
            DirectoryInfo di = new DirectoryInfo(inicio);

            foreach (var fi in di.GetDirectories())
            {
                 int tf = fi.GetFiles().Length;
                 int tp = fi.GetDirectories().Length;

                 Console.WriteLine("Delete: {0}, Total arquivos: {1}, total pastas: {2}", fi.FullName, tf.ToString(), tp.ToString());
                 processaDiretorio(fi.FullName);

                 if (fi.GetFiles().Length == 0 && fi.GetDirectories().Length == 0)
                 {
                     Console.WriteLine("Deleteing {0}...", fi.FullName);
                 }
                 else
                 {
                     Console.WriteLine("No delete {0}", fi.FullName);
                 }
            }
        }
    }
}
    
09.03.2016 / 17:02