I’m trying to move files zeroed (no content 0kb), I need help!

Asked

Viewed 32 times

-4

By pressing the button I need to move all zeroed files from one directory to another.

My code.

private void button1_Click(object sender, EventArgs e) {
            
var pasta = (@"C:\Users\Desktop\Nova pasta\");


var pasta2 = (@"C:\Users\Desktop\Nova pasta\outro\");

  var dir = new DirectoryInfo(pasta);
            
   foreach (FileInfo arquiv in dir.GetFiles()){

   if (arquiv.Length <= 0)
                    
{
  arquiv.MoveTo(pasta2);

                 }
             }
                
         }

1 answer

-1


You will use:

using System.IO;
using System.Linq;
using System.Collections.Generic;

First you need to check the existing files in the folder, something like

var arquivos = Directory.EnumerateFiles(pasta).ToArray();

will be able to do it for you.

After, you will only have to take the file name (Regex can help you with this).

Next you can use a conditional within a loop like:

foreach(var arquivo in arquivos)
    if (new FileInfo(arquivo).Length < 1)
    {
        File.Move(arquivo, pasta2 + arquivo);
    }

With this you will be able to compare the size of the file, as it is in a loop, at each loop the comparison will be made with the file corresponding to the position of the loop and if it meets its conditions, moved.

  • Thanks friend, it helped me a lot.

Browser other questions tagged

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