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

Asked

Viewed 104 times

4

I did this routine to delete empty folders, 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 for one problem. When I have a folder with only one file .zip inside, it deletes the Folder, because it thinks the Folder is empty. How I outline it?

Ex: I have this folder tree:

web\ws\tiss\v3\02\00

And inside the briefcase 00, I have the file TISS.zip. Ws folder and all its contents (subfolders) are deleted.

string[] files_new = Directory.GetFiles(path_files, "*", SearchOption.AllDirectories);
string[] folder_new = Directory.GetDirectories(path_files, "*", SearchOption.AllDirectories);

I did this function and continues to delete folders and subfolder, if at last there is only one file .zip.

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 changed the method to this one, using DirectoryInfo and yet I can’t get the file .zip in the folders.

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

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

foreach(var file in new DirectoryInfo(path_files).GetFiles())
{ 
    string s = file.Name;
}
  • What is the content of folder_new?

  • u@rubStackOverflow, A list of directories. All directories from a base folder. I made an edit in the original post.

  • I don’t know much about C#, but from what I’ve noticed you’re using Directory, Tell me what happens if you use DirectoryInfo, thus: DirectoryInfo di = new DirectoryInfo(@"C:\MinhaPasta");


 Console.WriteLine("Search pattern AllDirectories returns:");
 foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
 {
 Console.WriteLine(fi.Name);
 }

  • @Guilhermenascimento, good morning. I had to pause this part to solve a problem in the company and as soon as I return, I will test what you passed me for comment. Just a little while longer.

  • @Guilhermenascimento, you practically said everything. You should use the Directoryinfo class and not the Directory. I just need to improve my method using Directoryinfo.

1 answer

1


I don’t know much about C#, but in the documentation Directory.GetDirectories did not have parameters, but the parameters mentioned directed to documentation of the DirectoryInfo, try to do 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();
         }
    }
}

View 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);
                 }
            }
        }
    }
}
  • If I do, I find any. zip file in the folder: foreach(var file in new DirectoryInfo(path_files).GetFiles()) { }. But the way you answered, I can’t find a file .zip inside the folders.

  • @pnet I will test as soon as possible and give you a feedback

  • I did several ways and could not yet, merge what is working and delete the empty folder and save the folder with files.

  • @pnet really do not know why this occurs, see I posted a full example code in the reply, test it please.

Browser other questions tagged

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