0

I have n no. of files with many extension in local folder. I have to read count of total count of files from folder, again I have to check size of every single file, after verifying size.. .if we got any file size <0 byte . I have to report it as corrupted or could not open that file... so we can check that file by opening of every single files from folder...

I have tried following code to get count but with only one extension type. So how can get count of files from folder with all extension and how I can check size of file. And how I check whether that is corrupted. If I can open it then its sufficient.

public static int GetFileCount(string path, string searchPattern, SearchOption searchOption)
{
    var fileCount = 0;
    var fileIter = Directory.EnumerateFiles(path, searchPattern, searchOption);
    foreach (var file in fileIter)
        fileCount++;

    return fileCount;
}

called above function as

GetFileCount(@"D:\Attachment", "*.png", SearchOption.AllDirectories);
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Clark Mike
  • 49
  • 2
  • 2
  • 8
  • Read up on [DirectoryInfo](https://msdn.microsoft.com/en-us/library/System.IO.DirectoryInfo%28v=vs.100%29.aspx) and [FileInfo](https://msdn.microsoft.com/en-us/library/system.io.fileinfo%28v=vs.100%29.aspx), work with them and you should be easily on your way. – Bernd Linde Apr 17 '15 at 15:32

3 Answers3

0

So firstly to count the files in a given folder then loop over each file.

DirectoryInfo di = new DirectoryInfo("");

foreach (FileInfo fi in di.GetFiles())
{
    if(fi.Length ==0)
    {
        //must be corrupt
    }
}
Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12
0

You can do something similar to the following. I realize that having a container class for the filename, extension and length is somewhat redundant because a FileInfo object already contains this information, but having a container allows for the option of having additional custom fields and properties.

The following should compile:

class Program
{
    static void Main(string[] args)
    {
        var details = GetFileDetails(@"c:\", "*.*", SearchOption.AllDirectories);
        //var details = GetFileDetails(@"c:\", "*.png", SearchOption.AllDirectories);

        var filesOfZeroSize = details.Where(detail => detail.Length <= 0).ToList();
        var filesOfZeroSizeCount = filesOfZeroSize.Count;

        if (filesOfZeroSizeCount > 0)
        {
            Console.WriteLine("There are {0} files with a length of 0.", filesOfZeroSizeCount);
        }
    }

    public static List<FileDetail> GetFileDetails(string path, string searchPattern, SearchOption options)
    {
        var directory = new DirectoryInfo(path);

        if (!directory.Exists)
        {
            throw new DirectoryNotFoundException("Path");
        }

        List<FileDetail> fileDetails = new List<FileDetail>();
        FileInfo[] files = directory.GetFiles(searchPattern, options);
        foreach (FileInfo fileInfo in files)
        {
            fileDetails.Add(new FileDetail(fileInfo.Name, fileInfo.Extension, fileInfo.Length));
        }

        return fileDetails;
    }
}

class FileDetail
{
    public string FileName { get; set; }

    public string Extension { get; set; }

    public long Length { get; set; }

    public FileDetail()
        : this(null, null, -1)
    {
    }

    public FileDetail(string fileName, string extension, long length)
    {
        this.FileName = fileName;
        this.Extension = extension;
        this.Length = length;
    }
}

Keep in mind, you might run into an access denied exception when using the above solution. In which case, consider reviewing the accepted answer of the following post and modifying the GetFileDetails method to accommodate for this.

Access to the path is denied when using Directory.GetFiles(…)

Community
  • 1
  • 1
James Shaw
  • 839
  • 1
  • 6
  • 16
  • Very very nice post ...i got exactly my expected result...thanks much – Clark Mike Apr 18 '15 at 16:33
  • Now in next step ...i have to read folder sequentially and each file in every single folder ....and then have to check same ...count of all files from all folder, size of each file....so how i can read any folder file within that folder ... from this code i can only read files from @"D:\Attachment folder..................so what i want ...i don't know name of folder also ...but i know directory is D;....so how to do this.. var details = GetFileDetails(@"D:\Attachment", "*.*", SearchOption.AllDirectories); – Clark Mike Apr 18 '15 at 16:51
  • If my post was helpful, please mark it as the answer. Otherwise, tell how it has failed so I can update the answer. Thanks! :) – James Shaw Apr 20 '15 at 13:15
  • If I am understanding correctly, you would do the following: var details = GetFileDetails(@"D:\", "*.*", SearchOption.AllDirectories); – James Shaw Apr 20 '15 at 13:29
  • Please explain as to what you mean by you can only read said folder? Is there an error thrown? – James Shaw Apr 20 '15 at 13:30
  • No your post was really helpful ...no doubt ...only i am having little bit further modifications in that....like every time we were passing directory and folder name as parameter e.g @"D:\Attachment ...so instead of that ...if i have to pass https:///sharepoint/document/libarary reference and then have to read all folders within that ..and also files under that too...so my question is will our same code work....because ...when i pass http link as folder or directory path ...it is saying bad formation of path ...how to proceed with same code ...could you please help me more – Clark Mike Apr 20 '15 at 15:27
  • This is because you are passing in a http address instead of a folder location. You never mentioned that you needed it to iterate through a directory pointed to by a web address. See the following for further detail http://stackoverflow.com/questions/12876442/iterate-through-a-site-relative-path-to-find-files-asp-net-mvc-3 – James Shaw Apr 20 '15 at 21:12
  • yeah that was my mistake...but above code is working perfect ..i just want to pass web address instead of folder path.....also gone thru this link ..http://stackoverflow.com/questions/12876442/iterate-through-a-site-relative-path-to-find-files-asp-net-mvc-3......not helped too much .... – Clark Mike Apr 21 '15 at 04:20
0

Try using this method, Just pass the directory of the path and you can see the file names with extensions and their size separated by comma in the console.

   public static void filesInfoInDir(string dirPath)
    {

        try
        {

            foreach (string f in Directory.GetFiles(dirPath))
            {

                FileInfo file = new FileInfo(Path.Combine(dirPath,f));
                long s1 = file.Length;
      //This will write the file names along with their size separated with a comma
                Console.WriteLine(f + "," + s1);


            }

        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }


    }
vishnulphb
  • 61
  • 1
  • 5