What's wrong with my code?

0

I want to make a program that checks if any file is null, but is throwing an exception, what's wrong with my code ?:

    static List<string> GetFiles(string path)
    {

        string[] directories = Directory.GetDirectories(path);

        List<string> filesList = new List<string>();

        foreach(string directory in directories)
        {
            string[] files = Directory.GetFiles(directory);

            foreach(string f in files)
            {
                filesList.Add(directories + "\" + f);
            }
        }
        return filesList;
    }

    private void txtB_Path_TextChanged(object sender, EventArgs e) {  }

    private void bt_Start_Click(object sender, EventArgs e)
    {
        List<string> files = GetFiles(txtB_Path.Text);

        List<string> nullFiles = new List<string>();

        List<string> nullFilesDirectories = new List<string>();

        try
        {
            foreach (string item in files)
            {

                byte[] fileBytes = File.ReadAllBytes(item);

                foreach (byte bytes in fileBytes)
                {
                    if (bytes == 0)
                    {
                        nullFiles.Add(fileBytes.ToString());
                        nullFilesDirectories.Add(item);
                    }
                }
            }
            lb_NullFiles.Text = nullFiles.Count.ToString();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
            lb_NullFiles.Text = "0";
        }

        try {
            if (chckB_DeleteNullFiles.Checked)
            {
                foreach (string file in nullFilesDirectories)
                {
                    File.Delete(file);
                }
            }
        }
        catch
        {
            MessageBox.Show("Could not delete files");
        }
        lb_Files.Text = files.Count.ToString();

    }
    
asked by anonymous 24.02.2018 / 01:05

2 answers

1

Perhaps there is confusion there as to whether a file is null. I understand that it is not possible, the file can be empty / blank / etc ...

Considering that you only want to search for files in a folder, and delete all files that are empty, that is, size = 0, you can use the following code:

bool delArquivosVazios = true; //valor do seu checkbox
string[] files = Directory.GetFiles("c:\pasta\", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
    FileInfo obj = new FileInfo(file);
    if (obj.Length == 0 && delArquivosVazios)
        obj.Delete();
}

You can still specify a filter:

  

Change "*.*" to "*.txt" to search for text files, for example.

You can also change the search option:

  

SearchOption.AllDirectories For the given folder and all its subdirectories

     

SearchOption.TopDirectoryOnly For only the briefed folder.

    
24.02.2018 / 03:42
0

You are adding in the list of null files the contents of the file, rather than the path of it.

In addition, I believe you do not need that internal loop by reading each byte of the file ... As it stands, it is quite possible that all files in the directory are deleted because they may contain a '0' byte.

I believe this is your intention:

try
{
    foreach (string item in files)
    {
        byte[] fileBytes = File.ReadAllBytes(item);

        if (string.IsNullOrWhitespace(fileBytes.ToString())
        {    
             nullFiles.Add(fileBytes.ToString);
             lb_NullFiles.Text = nullFiles.Count.ToString();

        }
   } 
   catch(Exception ex)
   {
       MessageBox.Show(ex.Message);
       lb_NullFiles.Text = "0";
   }
} 
    
24.02.2018 / 01:48