counting files that contain a certain string in the filename?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    counting files that contain a certain string in the filename?

    I need to count files in a certain directory that has the string -Contract
    Template.xml at the end of it. How would I do this?



  • Stephany Young

    #2
    Re: counting files that contain a certain string in the filename?

    Try something like:

    Dim _files = Directory.GetFi les(_path)

    Dim _count = 0

    For Each _file In _files
    If _file.EndsWith( "-Contract Template.xml") then _count += 1
    Next


    "Andy B" <a_borka@sbcglo bal.netwrote in message
    news:e127w2k2IH A.4672@TK2MSFTN GP04.phx.gbl...
    >I need to count files in a certain directory that has the string -Contract
    >Template.xml at the end of it. How would I do this?
    >
    >
    >

    Comment

    • rowe_newsgroups

      #3
      Re: counting files that contain a certain string in the filename?

      On Jun 29, 9:28 pm, "Stephany Young" <noone@localhos twrote:
      Try something like:
      >
        Dim _files = Directory.GetFi les(_path)
      >
        Dim _count = 0
      >
        For Each _file In _files
          If _file.EndsWith( "-Contract Template.xml") then _count += 1
        Next
      >
      "Andy B" <a_bo...@sbcglo bal.netwrote in message
      >
      news:e127w2k2IH A.4672@TK2MSFTN GP04.phx.gbl...
      >
      I need to count files in a certain directory that has the string -Contract
      Template.xml at the end of it. How would I do this?
      Or you could do it without the loop:

      /////////////////////////
      Dim fileCount As Integer = Directory.GetFi les(path, "*-Contract
      Template.xml"). Length
      /////////////////////////

      You also have the option to search sub directories if need be:

      /////////////////////////
      Dim totalFileCount As Integer = Directory.GetFi les(path, "*-Contract
      Template.xml", SearchOption.Al lDirectories).L ength
      /////////////////////////

      Thanks,

      Seth Rowe [MVP]

      Comment

      • Stephany Young

        #4
        Re: counting files that contain a certain string in the filename?

        Yes, correct Seth, but you could get bitten by the undesirable 3 character
        extension behaviour that is documented in the documentation for the
        Directory.GetFi les method.

        Given a directory with files named:

        A123-Contract Template.xml
        A123-Contract Template.xmla

        using:

        Directory.GetFi les(path, "*-Contract Template.xml"). Length

        would result in a count of 2, whereas using a loop will get the correct
        count.


        "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
        news:37fc6de7-a5eb-4506-ac67-69d52c199121@i7 6g2000hsf.googl egroups.com...
        On Jun 29, 9:28 pm, "Stephany Young" <noone@localhos twrote:
        Try something like:
        >
        Dim _files = Directory.GetFi les(_path)
        >
        Dim _count = 0
        >
        For Each _file In _files
        If _file.EndsWith( "-Contract Template.xml") then _count += 1
        Next
        >
        "Andy B" <a_bo...@sbcglo bal.netwrote in message
        >
        news:e127w2k2IH A.4672@TK2MSFTN GP04.phx.gbl...
        >
        I need to count files in a certain directory that has the
        string -Contract
        Template.xml at the end of it. How would I do this?
        Or you could do it without the loop:

        /////////////////////////
        Dim fileCount As Integer = Directory.GetFi les(path, "*-Contract
        Template.xml"). Length
        /////////////////////////

        You also have the option to search sub directories if need be:

        /////////////////////////
        Dim totalFileCount As Integer = Directory.GetFi les(path, "*-Contract
        Template.xml", SearchOption.Al lDirectories).L ength
        /////////////////////////

        Thanks,

        Seth Rowe [MVP]

        Comment

        • rowe_newsgroups

          #5
          Re: counting files that contain a certain string in the filename?

          Yes, correct Seth, but you could get bitten by the undesirable 3 character
          extension behaviour that is documented in the documentation for the
          Directory.GetFi les method.
          >
          Given a directory with files named:
          >
            A123-Contract Template.xml
            A123-Contract Template.xmla
          >
          using:
          >
            Directory.GetFi les(path, "*-Contract Template.xml"). Length
          >
          would result in a count of 2, whereas using a loop will get the correct
          count.
          Ahh, very interesting. I guess I would have known about that one if I
          ever read the documentation :-)

          That also explain why you posted the for...loop and not a sample using
          the overload, as with your experience I knew you would know about the
          passing in the search pattern. Thanks again for explaining why the
          overload is dangerous and also why you posted what you did.

          Thanks,

          Seth Rowe [MVP]


          Comment

          • Andy B

            #6
            Re: counting files that contain a certain string in the filename?

            This is right. I needed something with the EndsWith method since I wanted to
            ignore files with extensions like xmla and all those oddballs that would
            possibly pose as a security problem as well as invalid file formats. Only
            files that end in "-Contract Template.xml" are valid filenames. The next
            part of a valid file for loading is to make sure the files with the
            "-Contract Template.xml" ending conform to a particular xml schema that is
            created with a dataset. Just having a particular ending won't work. Since I
            have the filename test done (it did work btw), now it's time to move on to
            the valid xml schema is/is not valid test. Another post said to just try
            loading the files into a dataset in a try...catch block. Is this right? or
            is there a better way to do it?


            "Stephany Young" <noone@localhos twrote in message
            news:uzs7yeq2IH A.3780@TK2MSFTN GP05.phx.gbl...
            Yes, correct Seth, but you could get bitten by the undesirable 3 character
            extension behaviour that is documented in the documentation for the
            Directory.GetFi les method.
            >
            Given a directory with files named:
            >
            A123-Contract Template.xml
            A123-Contract Template.xmla
            >
            using:
            >
            Directory.GetFi les(path, "*-Contract Template.xml"). Length
            >
            would result in a count of 2, whereas using a loop will get the correct
            count.
            >
            >
            "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
            news:37fc6de7-a5eb-4506-ac67-69d52c199121@i7 6g2000hsf.googl egroups.com...
            On Jun 29, 9:28 pm, "Stephany Young" <noone@localhos twrote:
            >Try something like:
            >>
            >Dim _files = Directory.GetFi les(_path)
            >>
            >Dim _count = 0
            >>
            >For Each _file In _files
            >If _file.EndsWith( "-Contract Template.xml") then _count += 1
            >Next
            >>
            >"Andy B" <a_bo...@sbcglo bal.netwrote in message
            >>
            >news:e127w2k2I HA.4672@TK2MSFT NGP04.phx.gbl.. .
            >>
            >I need to count files in a certain directory that has the
            >string -Contract
            >Template.xml at the end of it. How would I do this?
            >
            Or you could do it without the loop:
            >
            /////////////////////////
            Dim fileCount As Integer = Directory.GetFi les(path, "*-Contract
            Template.xml"). Length
            /////////////////////////
            >
            You also have the option to search sub directories if need be:
            >
            /////////////////////////
            Dim totalFileCount As Integer = Directory.GetFi les(path, "*-Contract
            Template.xml", SearchOption.Al lDirectories).L ength
            /////////////////////////
            >
            Thanks,
            >
            Seth Rowe [MVP]

            Comment

            • rowe_newsgroups

              #7
              Re: counting files that contain a certain string in the filename?

              On Jun 30, 9:48 am, "Andy B" <a_bo...@sbcglo bal.netwrote:
              This is right. I needed something with the EndsWith method since I wanted to
              ignore files with extensions like xmla and all those oddballs that would
              possibly pose as a security problem as well as invalid file formats. Only
              files that end in "-Contract Template.xml" are valid filenames. The next
              part of a valid file for loading is to make sure the files with the
              "-Contract Template.xml" ending conform to a particular xml schema that is
              created with a dataset. Just having a particular ending won't work. Since I
              have the filename test done (it did work btw), now it's time to move on to
              the valid xml schema is/is not valid test. Another post said to just try
              loading the files into a dataset in a try...catch block. Is this right? or
              is there a better way to do it?
              >
              "Stephany Young" <noone@localhos twrote in message
              >
              news:uzs7yeq2IH A.3780@TK2MSFTN GP05.phx.gbl...
              >
              Yes, correct Seth, but you could get bitten by the undesirable 3 character
              extension behaviour that is documented in the documentation for the
              Directory.GetFi les method.
              >
              Given a directory with files named:
              >
               A123-Contract Template.xml
               A123-Contract Template.xmla
              >
              using:
              >
               Directory.GetFi les(path, "*-Contract Template.xml"). Length
              >
              would result in a count of 2, whereas using a loop will get the correct
              count.
              >
              "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
              news:37fc6de7-a5eb-4506-ac67-69d52c199121@i7 6g2000hsf.googl egroups.com...
              On Jun 29, 9:28 pm, "Stephany Young" <noone@localhos twrote:
              Try something like:
              >
              Dim _files = Directory.GetFi les(_path)
              >
              Dim _count = 0
              >
              For Each _file In _files
              If _file.EndsWith( "-Contract Template.xml") then _count += 1
              Next
              >
              "Andy B" <a_bo...@sbcglo bal.netwrote in message
              >
              >news:e127w2k2I HA.4672@TK2MSFT NGP04.phx.gbl.. .
              >
              I need to count files in a certain directory that has the
              string -Contract
              Template.xml at the end of it. How would I do this?
              >
              Or you could do it without the loop:
              >
              /////////////////////////
              Dim fileCount As Integer = Directory.GetFi les(path, "*-Contract
              Template.xml"). Length
              /////////////////////////
              >
              You also have the option to search sub directories if need be:
              >
              /////////////////////////
              Dim totalFileCount As Integer = Directory.GetFi les(path, "*-Contract
              Template.xml", SearchOption.Al lDirectories).L ength
              /////////////////////////
              >
              Thanks,
              >
              Seth Rowe [MVP]
              .NET actually exposes a class / method to validate an Xml file against
              a schema. It's been a long, long time since I've used it, but it's
              called something like XmlSchemaValida tion. If you can't find it let me
              know and I'll try to find the project I used it on. This should be
              substantially lighter weight than creating a dataset/datatable and
              using a try...catch block.

              Thanks,

              Seth Rowe [MVP]

              Comment

              • rowe_newsgroups

                #8
                Re: counting files that contain a certain string in the filename?

                On Jun 30, 11:34 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
                On Jun 30, 9:48 am, "Andy B" <a_bo...@sbcglo bal.netwrote:
                >
                >
                >
                This is right. I needed something with the EndsWith method since I wanted to
                ignore files with extensions like xmla and all those oddballs that would
                possibly pose as a security problem as well as invalid file formats. Only
                files that end in "-Contract Template.xml" are valid filenames. The next
                part of a valid file for loading is to make sure the files with the
                "-Contract Template.xml" ending conform to a particular xml schema that is
                created with a dataset. Just having a particular ending won't work. Since I
                have the filename test done (it did work btw), now it's time to move on to
                the valid xml schema is/is not valid test. Another post said to just try
                loading the files into a dataset in a try...catch block. Is this right? or
                is there a better way to do it?
                >
                "Stephany Young" <noone@localhos twrote in message
                >
                news:uzs7yeq2IH A.3780@TK2MSFTN GP05.phx.gbl...
                >
                Yes, correct Seth, but you could get bitten by the undesirable 3 character
                extension behaviour that is documented in the documentation for the
                Directory.GetFi les method.
                >
                Given a directory with files named:
                >
                 A123-Contract Template.xml
                 A123-Contract Template.xmla
                >
                using:
                >
                 Directory.GetFi les(path, "*-Contract Template.xml"). Length
                >
                would result in a count of 2, whereas using a loop will get the correct
                count.
                >
                "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                >news:37fc6de 7-a5eb-4506-ac67-69d52c199121@i7 6g2000hsf.googl egroups.com....
                On Jun 29, 9:28 pm, "Stephany Young" <noone@localhos twrote:
                >Try something like:
                >
                >Dim _files = Directory.GetFi les(_path)
                >
                >Dim _count = 0
                >
                >For Each _file In _files
                >If _file.EndsWith( "-Contract Template.xml") then _count += 1
                >Next
                >
                >"Andy B" <a_bo...@sbcglo bal.netwrote in message
                >
                >>news:e127w2k2 IHA.4672@TK2MSF TNGP04.phx.gbl. ..
                >
                >I need to count files in a certain directory that has the
                >string -Contract
                >Template.xml at the end of it. How would I do this?
                >
                Or you could do it without the loop:
                >
                /////////////////////////
                Dim fileCount As Integer = Directory.GetFi les(path, "*-Contract
                Template.xml"). Length
                /////////////////////////
                >
                You also have the option to search sub directories if need be:
                >
                /////////////////////////
                Dim totalFileCount As Integer = Directory.GetFi les(path, "*-Contract
                Template.xml", SearchOption.Al lDirectories).L ength
                /////////////////////////
                >
                Thanks,
                >
                Seth Rowe [MVP]
                >
                .NET actually exposes a class / method to validate an Xml file against
                a schema. It's been a long, long time since I've used it, but it's
                called something like XmlSchemaValida tion. If you can't find it let me
                know and I'll try to find the project I used it on. This should be
                substantially lighter weight than creating a dataset/datatable and
                using a try...catch block.
                >
                Thanks,
                >
                Seth Rowe [MVP]
                Or maybe it was XmlValidatingRe ader, I don't really remember :-(

                Thanks,

                Seth Rowe [MVP]

                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: counting files that contain a certain string in the filename?

                  Stephany,
                  I would consider combining the methods:

                  Dim _files = Directory.GetFi les(_path, "*-Contract Template.xml")
                  Dim _count = 0

                  For Each _file In _files
                  If _file.EndsWith( "-Contract Template.xml") then _count += 1
                  Next

                  As this lets the OS do the heavy lifting for the bulk of the files names in
                  a folder allowing the for each to iterate over a subset of all files on
                  disk. Read Directory.GetFi les will return a smaller array...

                  I would also consider using LINQ:

                  Dim _count = Aggregate file In ( _
                  From file In Directory.GetFi les(path, "*" &
                  "-Contract Template.xml") _
                  Where file.EndsWith("-Contract Template.xml") _
                  ) Into Count()


                  --
                  Hope this helps
                  Jay B. Harlow
                  ..NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net


                  "Stephany Young" <noone@localhos twrote in message
                  news:eWIWmCl2IH A.5564@TK2MSFTN GP06.phx.gbl...
                  Try something like:
                  >
                  Dim _files = Directory.GetFi les(_path)
                  >
                  Dim _count = 0
                  >
                  For Each _file In _files
                  If _file.EndsWith( "-Contract Template.xml") then _count += 1
                  Next
                  >
                  >
                  "Andy B" <a_borka@sbcglo bal.netwrote in message
                  news:e127w2k2IH A.4672@TK2MSFTN GP04.phx.gbl...
                  >>I need to count files in a certain directory that has the string -Contract
                  >>Template.xm l at the end of it. How would I do this?
                  >>
                  >>
                  >>
                  >

                  Comment

                  • Stephany Young

                    #10
                    Re: counting files that contain a certain string in the filename?

                    Well, yes Jay. You could also consider a myriad of other techniques as
                    well. Not that I had said 'try something like', meaning 'similar to' or 'a
                    variation of'.

                    I attempted to post a follow up to another post of mine on another branch of
                    this thread, but for some reason or another it doesn't appear in the thread
                    (in my reader anyway), that pointed out the gotcha when using the * wildcard
                    character with Directory.GetFi les(_path, _pattern) and the _pattern
                    specifies a 3 character extension, e.g., Directory.GetFi les(_path, "*.xml").
                    This gotcha is spelled out in the documentation for the
                    Directory.GetFi les(_path, _pattern) method.

                    I had always interpreted that as meaning that the gotcha applied if one used
                    the * wildcard character anywhere to the left of the . character. However, a
                    test I executed a few hours ago shows that it only apply when the pattern
                    is, in fact, in the form "*.<3 character extension>".

                    In the OP's case, this renders the loop redundant because:

                    Directory.GetFi les(_path, "*-Contract Template.xml")

                    will return the correct result.

                    If the call was:

                    Directory.GetFi les(_path, "*.xml")

                    then the result would be incorrect if, and only if, the directory contained
                    other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.

                    Even we old hands can stand educated sometimes :)

                    Now I certainly appreciate your enthusiasm and evangelism and although it is
                    a valid technique, don't you think LINQ is a bit over the top for the task
                    at hand. :)


                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.netw rote in
                    message news:ujzD%23Ww2 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
                    Stephany,
                    I would consider combining the methods:
                    >
                    Dim _files = Directory.GetFi les(_path, "*-Contract Template.xml")
                    Dim _count = 0
                    >
                    For Each _file In _files
                    If _file.EndsWith( "-Contract Template.xml") then _count += 1
                    Next
                    >
                    As this lets the OS do the heavy lifting for the bulk of the files names
                    in a folder allowing the for each to iterate over a subset of all files on
                    disk. Read Directory.GetFi les will return a smaller array...
                    >
                    I would also consider using LINQ:
                    >
                    Dim _count = Aggregate file In ( _
                    From file In Directory.GetFi les(path, "*" &
                    "-Contract Template.xml") _
                    Where file.EndsWith("-Contract Template.xml") _
                    ) Into Count()
                    >
                    >
                    --
                    Hope this helps
                    Jay B. Harlow
                    .NET Application Architect, Enthusiast, & Evangelist
                    T.S. Bradley - http://www.tsbradley.net
                    >
                    >
                    "Stephany Young" <noone@localhos twrote in message
                    news:eWIWmCl2IH A.5564@TK2MSFTN GP06.phx.gbl...
                    >Try something like:
                    >>
                    > Dim _files = Directory.GetFi les(_path)
                    >>
                    > Dim _count = 0
                    >>
                    > For Each _file In _files
                    > If _file.EndsWith( "-Contract Template.xml") then _count += 1
                    > Next
                    >>
                    >>
                    >"Andy B" <a_borka@sbcglo bal.netwrote in message
                    >news:e127w2k2I HA.4672@TK2MSFT NGP04.phx.gbl.. .
                    >>>I need to count files in a certain directory that has the
                    >>>string -Contract Template.xml at the end of it. How would I do this?
                    >>>
                    >>>
                    >>>
                    >>
                    >

                    Comment

                    • Andy B

                      #11
                      Re: counting files that contain a certain string in the filename?

                      Directory.GetFi les(_path, "*-Contract Template.xml")

                      If this is a better way of doing things, then I probably will try this way
                      instead. Makes more sense to use it this way since I have to find all the
                      files with that partial name and then validate them to make sure they aren't
                      a fake of some kind.



                      "Stephany Young" <noone@localhos twrote in message
                      news:%23tOKPrx2 IHA.1808@TK2MSF TNGP04.phx.gbl. ..
                      Well, yes Jay. You could also consider a myriad of other techniques as
                      well. Not that I had said 'try something like', meaning 'similar to' or
                      'a variation of'.
                      >
                      I attempted to post a follow up to another post of mine on another branch
                      of this thread, but for some reason or another it doesn't appear in the
                      thread (in my reader anyway), that pointed out the gotcha when using the *
                      wildcard character with Directory.GetFi les(_path, _pattern) and the
                      _pattern specifies a 3 character extension, e.g.,
                      Directory.GetFi les(_path, "*.xml"). This gotcha is spelled out in the
                      documentation for the Directory.GetFi les(_path, _pattern) method.
                      >
                      I had always interpreted that as meaning that the gotcha applied if one
                      used the * wildcard character anywhere to the left of the . character.
                      However, a test I executed a few hours ago shows that it only apply when
                      the pattern is, in fact, in the form "*.<3 character extension>".
                      >
                      In the OP's case, this renders the loop redundant because:
                      >
                      Directory.GetFi les(_path, "*-Contract Template.xml")
                      >
                      will return the correct result.
                      >
                      If the call was:
                      >
                      Directory.GetFi les(_path, "*.xml")
                      >
                      then the result would be incorrect if, and only if, the directory
                      contained other files with extensions that start with .xml, e.g., .xmla,
                      .xmlb, etc.
                      >
                      Even we old hands can stand educated sometimes :)
                      >
                      Now I certainly appreciate your enthusiasm and evangelism and although it
                      is a valid technique, don't you think LINQ is a bit over the top for the
                      task at hand. :)
                      >
                      >
                      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.netw rote in
                      message news:ujzD%23Ww2 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
                      >Stephany,
                      >I would consider combining the methods:
                      >>
                      > Dim _files = Directory.GetFi les(_path, "*-Contract Template.xml")
                      > Dim _count = 0
                      >>
                      > For Each _file In _files
                      > If _file.EndsWith( "-Contract Template.xml") then _count += 1
                      > Next
                      >>
                      >As this lets the OS do the heavy lifting for the bulk of the files names
                      >in a folder allowing the for each to iterate over a subset of all files
                      >on disk. Read Directory.GetFi les will return a smaller array...
                      >>
                      >I would also consider using LINQ:
                      >>
                      >Dim _count = Aggregate file In ( _
                      > From file In Directory.GetFi les(path, "*" &
                      >"-Contract Template.xml") _
                      > Where file.EndsWith("-Contract Template.xml") _
                      > ) Into Count()
                      >>
                      >>
                      >--
                      >Hope this helps
                      >Jay B. Harlow
                      >.NET Application Architect, Enthusiast, & Evangelist
                      >T.S. Bradley - http://www.tsbradley.net
                      >>
                      >>
                      >"Stephany Young" <noone@localhos twrote in message
                      >news:eWIWmCl2I HA.5564@TK2MSFT NGP06.phx.gbl.. .
                      >>Try something like:
                      >>>
                      >> Dim _files = Directory.GetFi les(_path)
                      >>>
                      >> Dim _count = 0
                      >>>
                      >> For Each _file In _files
                      >> If _file.EndsWith( "-Contract Template.xml") then _count += 1
                      >> Next
                      >>>
                      >>>
                      >>"Andy B" <a_borka@sbcglo bal.netwrote in message
                      >>news:e127w2k2 IHA.4672@TK2MSF TNGP04.phx.gbl. ..
                      >>>>I need to count files in a certain directory that has the
                      >>>>string -Contract Template.xml at the end of it. How would I do this?
                      >>>>
                      >>>>
                      >>>>
                      >>>
                      >>
                      >

                      Comment

                      • rowe_newsgroups

                        #12
                        Re: counting files that contain a certain string in the filename?

                        In the OP's case, this renders the loop redundant because:
                        >
                          Directory.GetFi les(_path, "*-Contract Template.xml")
                        >
                        will return the correct result.
                        >
                        If the call was:
                        >
                          Directory.GetFi les(_path, "*.xml")
                        >
                        then the result would be incorrect if, and only if, the directory contained
                        other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.
                        Not sure about everyone else, but I am now thoroughly confused!

                        I guess I'll just wander back to my web development world and be happy
                        I rarely have to deal with Directory.GetFi les(...)

                        :-)

                        Thanks,

                        Seth Rowe [MVP]


                        Comment

                        • Andy B

                          #13
                          Re: counting files that contain a certain string in the filename?

                          Sorry to get you confused *sigh*... Didn't want to do anything like that...


                          "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                          news:2b7f1a95-85e6-4c1e-a455-ce9e7796992c@m4 4g2000hsc.googl egroups.com...
                          In the OP's case, this renders the loop redundant because:
                          >
                          Directory.GetFi les(_path, "*-Contract Template.xml")
                          >
                          will return the correct result.
                          >
                          If the call was:
                          >
                          Directory.GetFi les(_path, "*.xml")
                          >
                          then the result would be incorrect if, and only if, the directory
                          contained
                          other files with extensions that start with .xml, e.g., .xmla, .xmlb, etc.
                          Not sure about everyone else, but I am now thoroughly confused!

                          I guess I'll just wander back to my web development world and be happy
                          I rarely have to deal with Directory.GetFi les(...)

                          :-)

                          Thanks,

                          Seth Rowe [MVP]



                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: counting files that contain a certain string in the filename?

                            I would keep the loop for the day when Andy's successor changes the criteria
                            and breaks the algorithm. Of course there is a chance Andy's successor

                            I don't consider the LINQ over the top as I tend to prefer stating what I
                            want to do (aggregate the count of files) rather then how specifically to do
                            it... Also using LINQ its now easy to add other accumulators, such as total
                            size of the files...

                            Of course there's the danger of "learn how to use a hammer, and every thing
                            is a nail"...

                            --
                            Hope this helps
                            Jay B. Harlow
                            ..NET Application Architect, Enthusiast, & Evangelist
                            T.S. Bradley - http://www.tsbradley.net


                            "Stephany Young" <noone@localhos twrote in message
                            news:%23tOKPrx2 IHA.1808@TK2MSF TNGP04.phx.gbl. ..
                            Well, yes Jay. You could also consider a myriad of other techniques as
                            well. Not that I had said 'try something like', meaning 'similar to' or
                            'a variation of'.
                            >
                            I attempted to post a follow up to another post of mine on another branch
                            of this thread, but for some reason or another it doesn't appear in the
                            thread (in my reader anyway), that pointed out the gotcha when using the *
                            wildcard character with Directory.GetFi les(_path, _pattern) and the
                            _pattern specifies a 3 character extension, e.g.,
                            Directory.GetFi les(_path, "*.xml"). This gotcha is spelled out in the
                            documentation for the Directory.GetFi les(_path, _pattern) method.
                            >
                            I had always interpreted that as meaning that the gotcha applied if one
                            used the * wildcard character anywhere to the left of the . character.
                            However, a test I executed a few hours ago shows that it only apply when
                            the pattern is, in fact, in the form "*.<3 character extension>".
                            >
                            In the OP's case, this renders the loop redundant because:
                            >
                            Directory.GetFi les(_path, "*-Contract Template.xml")
                            >
                            will return the correct result.
                            >
                            If the call was:
                            >
                            Directory.GetFi les(_path, "*.xml")
                            >
                            then the result would be incorrect if, and only if, the directory
                            contained other files with extensions that start with .xml, e.g., .xmla,
                            .xmlb, etc.
                            >
                            Even we old hands can stand educated sometimes :)
                            >
                            Now I certainly appreciate your enthusiasm and evangelism and although it
                            is a valid technique, don't you think LINQ is a bit over the top for the
                            task at hand. :)
                            >
                            >
                            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.netw rote in
                            message news:ujzD%23Ww2 IHA.4800@TK2MSF TNGP02.phx.gbl. ..
                            >Stephany,
                            >I would consider combining the methods:
                            >>
                            > Dim _files = Directory.GetFi les(_path, "*-Contract Template.xml")
                            > Dim _count = 0
                            >>
                            > For Each _file In _files
                            > If _file.EndsWith( "-Contract Template.xml") then _count += 1
                            > Next
                            >>
                            >As this lets the OS do the heavy lifting for the bulk of the files names
                            >in a folder allowing the for each to iterate over a subset of all files
                            >on disk. Read Directory.GetFi les will return a smaller array...
                            >>
                            >I would also consider using LINQ:
                            >>
                            >Dim _count = Aggregate file In ( _
                            > From file In Directory.GetFi les(path, "*" &
                            >"-Contract Template.xml") _
                            > Where file.EndsWith("-Contract Template.xml") _
                            > ) Into Count()
                            >>
                            >>
                            >--
                            >Hope this helps
                            >Jay B. Harlow
                            >.NET Application Architect, Enthusiast, & Evangelist
                            >T.S. Bradley - http://www.tsbradley.net
                            >>
                            >>
                            >"Stephany Young" <noone@localhos twrote in message
                            >news:eWIWmCl2I HA.5564@TK2MSFT NGP06.phx.gbl.. .
                            >>Try something like:
                            >>>
                            >> Dim _files = Directory.GetFi les(_path)
                            >>>
                            >> Dim _count = 0
                            >>>
                            >> For Each _file In _files
                            >> If _file.EndsWith( "-Contract Template.xml") then _count += 1
                            >> Next
                            >>>
                            >>>
                            >>"Andy B" <a_borka@sbcglo bal.netwrote in message
                            >>news:e127w2k2 IHA.4672@TK2MSF TNGP04.phx.gbl. ..
                            >>>>I need to count files in a certain directory that has the
                            >>>>string -Contract Template.xml at the end of it. How would I do this?
                            >>>>
                            >>>>
                            >>>>
                            >>>
                            >>
                            >

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: counting files that contain a certain string in the filename?

                              Jay
                              >
                              Of course there's the danger of "learn how to use a hammer, and every
                              thing is a nail"...
                              >
                              Very good one, I did not know it, I will for sure use it in future,

                              Cor


                              Comment

                              Working...