6 Replies - 401 Views - Last Post: 30 September 2011 - 02:09 AM Rate Topic: -----

#1 thomas130  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-March 11

How to use count to search only certain files extension

Posted 28 September 2011 - 11:45 PM

Hi guys I'm using dotnetzip to gain how many files are within a zip the only problem is it also count the folders. How can I change it so it only counts .txt and .ini files or does'nt count folders.

Thanks Tom

The code so far is down below



 
int count; //variable to store number of items
            using (ZipFile zip = ZipFile.Read(knowledgeplanning))
                count = zip.Count;
            Console.WriteLine(count);



Is This A Good Question/Topic? 0
  • +

Replies To: How to use count to search only certain files extension

#2 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: How to use count to search only certain files extension

Posted 29 September 2011 - 12:32 AM

hi,
as i see in http://cheeso.member...ea91a64df15.htm ZipFile implements IEnumerable<ZipEntry>.
ZipEntry has properties: FileName and IsDirectory so you can use linq to count files:
zip.Where(file => file.IsDirectory && file.FileName.EndsWith(".ini")).Count();


i didn't test this code but i think it will lead you to solution

documentation is the best place to start when you have problems with getting information you want. If you want to know any other things about zip read documentation, properties and methods are well documented and most probably you'll find all information you want.

This post has been edited by wiero: 29 September 2011 - 12:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: How to use count to search only certain files extension

Posted 29 September 2011 - 02:28 AM

Great answer wiero, although you would want it to count when the object is NOT a directory and IS a ini OR a txt
IEnumerable<ZipFile> files = zip.Where(file => (!file.IsDirectory) && ((file.FileName.EndsWith(".ini")) ||(file.FileName.EndsWith("*.txt"))));


Was This Post Helpful? 1
  • +
  • -

#4 thomas130  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-March 11

Re: How to use count to search only certain files extension

Posted 29 September 2011 - 04:23 AM

I hope this doesn’t sound cheeky but could someone explain how that code works I still very new to c# and programming third day in fact :unsure:
Was This Post Helpful? 0
  • +
  • -

#5 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: How to use count to search only certain files extension

Posted 29 September 2011 - 04:44 AM

It is quiet a full on bit of code for day 3 :)

Basically it is shortening a load of code down into a lambda method using linq. If you want to find out about these at some point google them, but for so early in your programming career I think that it is probably a bit too soon.

The code is more or less doing this (it probably works a bit differently internally but this should give you an idea):
private IEnumerable<String> WhereZipFileIsNotDirectoryAndIsIniOrTxt()
{
    // holds files that match the specification
    List<ZipFile> filesThatMatchTheSpecification = new List<ZipFile>();

    // itterate all objects in zip
    foreach (ZipFile zipObject in zip)
    {
        // if not a directory AND the file name ends with *ini OR *.txt (this is provided as the lambda method in the linq example)
        if ((!zipObject.IsDirectory) &&
            ((file.FileName.EndsWith(".ini")) || (file.FileName.EndsWith("*.txt"))))
        {
            // add object to list
            filesThatMatchTheSpecification.Add(zipObject);
        }
    }

    // return all files that match the specification
    return filesThatMatchTheSpecification as IEnumerable<ZipFile>;
}


This post has been edited by ragingben: 29 September 2011 - 04:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: How to use count to search only certain files extension

Posted 29 September 2011 - 06:34 AM

Yes, LINQ is a bit much to learn right away.
Was This Post Helpful? 0
  • +
  • -

#7 thomas130  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-March 11

Re: How to use count to search only certain files extension

Posted 30 September 2011 - 02:09 AM

Could anyone post a link to learn all the theory about what I need to do. I can understand some of the syntax you using but not sure how to implement for my thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1