Dir() in Loop returns ""

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

    #1

    Dir() in Loop returns ""

    G'day All,
    I have a problem with this loop.
    There are a number of .txt files in 'myPath'.

    tmpFile = Dir(myPath & "\*.txt")
    'PROCESS FOLDER
    Do Until tmpFile = ""
    <lottsa code>
    <too much to post>
    tmpFile = Dir
    Loop

    The first 'tmpFile = Dir(myPath & "\*.txt")'
    returns the first .txt file however,
    when I get to the 'tmpFile = Dir' it returns
    a zero-length string ("").
    Can anyone give *any* ideas what, where I should
    look in the ensuing code for the problem.
    I've been looking for 2 days now and am at a
    total loss.

    tia
    build
  • J French

    #2
    Re: Dir() in Loop returns &quot;&quot;

    On Tue, 21 Sep 2004 20:06:38 +1000, build <build02@datafa st.net.au>
    wrote:
    [color=blue]
    >G'day All,
    >I have a problem with this loop.
    >There are a number of .txt files in 'myPath'.
    >
    >tmpFile = Dir(myPath & "\*.txt")
    >'PROCESS FOLDER
    >Do Until tmpFile = ""
    > <lottsa code>
    > <too much to post>
    > tmpFile = Dir
    >Loop
    >
    >The first 'tmpFile = Dir(myPath & "\*.txt")'
    >returns the first .txt file however,
    >when I get to the 'tmpFile = Dir' it returns
    >a zero-length string ("").
    >Can anyone give *any* ideas what, where I should
    >look in the ensuing code for the problem.
    >I've been looking for 2 days now and am at a
    >total loss.[/color]

    I can make a very good guess

    Somewhere in <lottsa code> you are using the Dir() function again
    - that is shafting your first Dir() loop

    Try reading all the .Txt file names into an Array and then processing
    each item in the Array
    Then dig into <lottsa code> and get rid of the culprit(s)

    Dir() is a very dangerous thing to use, especially in lower
    functions/subs

    Comment

    • build

      #3
      Re: Dir() in Loop returns &quot;&quot;

      build wrote:[color=blue]
      > G'day All,
      > I have a problem with this loop.
      > There are a number of .txt files in 'myPath'.
      >
      > tmpFile = Dir(myPath & "\*.txt")
      > 'PROCESS FOLDER
      > Do Until tmpFile = ""
      > <lottsa code>
      > <too much to post>
      > tmpFile = Dir
      > Loop
      >
      > The first 'tmpFile = Dir(myPath & "\*.txt")'
      > returns the first .txt file however,
      > when I get to the 'tmpFile = Dir' it returns
      > a zero-length string ("").
      > Can anyone give *any* ideas what, where I should
      > look in the ensuing code for the problem.
      > I've been looking for 2 days now and am at a
      > total loss.
      >
      > tia
      > build[/color]
      G'day All,
      OK looks like my question is not clear enough.
      What can I be doing in the code between the
      'Do Until tmpFile = ""' and the 'tmpFile = Dir'
      that would make the second 'Dir' return a zero
      length string?
      Is that clearer?
      tia
      build

      Comment

      • Rick Rothstein

        #4
        Re: Dir() in Loop returns &quot;&quot;

        > > I have a problem with this loop.[color=blue][color=green]
        > > There are a number of .txt files in 'myPath'.
        > >
        > > tmpFile = Dir(myPath & "\*.txt")
        > > 'PROCESS FOLDER
        > > Do Until tmpFile = ""
        > > <lottsa code>
        > > <too much to post>
        > > tmpFile = Dir
        > > Loop
        > >
        > > The first 'tmpFile = Dir(myPath & "\*.txt")'
        > > returns the first .txt file however,
        > > when I get to the 'tmpFile = Dir' it returns
        > > a zero-length string ("").
        > > Can anyone give *any* ideas what, where I should
        > > look in the ensuing code for the problem.
        > > I've been looking for 2 days now and am at a
        > > total loss.
        > >
        > > tia
        > > build[/color]
        > G'day All,
        > OK looks like my question is not clear enough.
        > What can I be doing in the code between the
        > 'Do Until tmpFile = ""' and the 'tmpFile = Dir'
        > that would make the second 'Dir' return a zero
        > length string?[/color]

        Using the Dir function with a path and filename (or without wildcards)
        for a directory that has only one or no files in it. Another possibility
        is a loop inside that <lottsa code> that uses Dir with a different
        path/filename argument and in where the loop exhausts all the files in
        that other Dir specified path/filename.
        [color=blue]
        > Is that clearer?[/color]

        Not really.<g> Is this happening every time in the loop? Only at certain
        times?

        Rick - MVP

        Comment

        • mickey

          #5
          Re: Dir() in Loop returns &quot;&quot;

          On Thu, 23 Sep 2004 07:22:23 +1000, build <build02@datafa st.net.au>
          you typed some letters in random order:
          [color=blue]
          >build wrote:[color=green]
          >> G'day All,
          >> I have a problem with this loop.
          >> There are a number of .txt files in 'myPath'.
          >>
          >> tmpFile = Dir(myPath & "\*.txt")
          >> 'PROCESS FOLDER
          >> Do Until tmpFile = ""
          >> <lottsa code>
          >> <too much to post>
          >> tmpFile = Dir
          >> Loop
          >>
          >> The first 'tmpFile = Dir(myPath & "\*.txt")'
          >> returns the first .txt file however,
          >> when I get to the 'tmpFile = Dir' it returns
          >> a zero-length string ("").
          >> Can anyone give *any* ideas what, where I should
          >> look in the ensuing code for the problem.
          >> I've been looking for 2 days now and am at a
          >> total loss.
          >>
          >> tia
          >> build[/color]
          >G'day All,
          >OK looks like my question is not clear enough.
          >What can I be doing in the code between the
          >'Do Until tmpFile = ""' and the 'tmpFile = Dir'
          >that would make the second 'Dir' return a zero
          >length string?
          >Is that clearer?
          >tia
          >build[/color]

          I use a similar piece of code to delete old log files
          Notice the start of the do/while loop it differs from yours
          Perhaps that's the problem?

          I just tested it again and killed 192 files with this...

          <code>

          Function Kill_Files(Pad As String, Datum As Date, Patroon As String)
          Dim MyFile As String

          MyFile = Dir(Pad & "\" & Patroon)
          Do While MyFile <> ""
          If FileDateTime(Pa d & "\" & MyFile) < Datum Then
          Kill (Pad & "\" & MyFile)
          End If
          MyFile = Dir ' Get next entry.
          Loop
          End Function

          </code>




          Groetjenz,

          Mickey
          --
          #### gewoan skrieve su ast ut seist ####

          Comment

          • build

            #6
            Re: Dir() in Loop returns &quot;&quot; [my answer]

            mickey wrote:[color=blue]
            > On Thu, 23 Sep 2004 07:22:23 +1000, build <build02@datafa st.net.au>
            > you typed some letters in random order:
            >
            >[color=green]
            >>build wrote:
            >>[color=darkred]
            >>>G'day All,
            >>>I have a problem with this loop.
            >>>There are a number of .txt files in 'myPath'.
            >>>
            >>>tmpFile = Dir(myPath & "\*.txt")
            >>>'PROCESS FOLDER
            >>>Do Until tmpFile = ""
            >>> <lottsa code>
            >>> <too much to post>
            >>> tmpFile = Dir
            >>>Loop
            >>>
            >>>The first 'tmpFile = Dir(myPath & "\*.txt")'
            >>>returns the first .txt file however,
            >>>when I get to the 'tmpFile = Dir' it returns
            >>>a zero-length string ("").
            >>>Can anyone give *any* ideas what, where I should
            >>>look in the ensuing code for the problem.
            >>>I've been looking for 2 days now and am at a
            >>>total loss.
            >>>
            >>>tia
            >>>build[/color]
            >>
            >>G'day All,
            >>OK looks like my question is not clear enough.
            >>What can I be doing in the code between the
            >>'Do Until tmpFile = ""' and the 'tmpFile = Dir'
            >>that would make the second 'Dir' return a zero
            >>length string?
            >>Is that clearer?
            >>tia
            >>build[/color]
            >
            >
            > I use a similar piece of code to delete old log files
            > Notice the start of the do/while loop it differs from yours
            > Perhaps that's the problem?
            >
            > I just tested it again and killed 192 files with this...
            >
            > <code>
            >
            > Function Kill_Files(Pad As String, Datum As Date, Patroon As String)
            > Dim MyFile As String
            >
            > MyFile = Dir(Pad & "\" & Patroon)
            > Do While MyFile <> ""
            > If FileDateTime(Pa d & "\" & MyFile) < Datum Then
            > Kill (Pad & "\" & MyFile)
            > End If
            > MyFile = Dir ' Get next entry.
            > Loop
            > End Function
            >
            > </code>
            >
            > Groetjenz,
            >
            > Mickey[/color]

            G'day Mickey,
            THANK YOU for your reply.
            Do While myFile "is not equal to empty string"
            is the same as:
            Do Until myFile "is equal to empty string"

            What I've done is not an answer but avoids the problem.

            Dim fileList() 'array to store filenames
            Dim i as Integer 'loop counter
            tmpFile = Dir(myPath & "\*.txt") 'get the first file name
            Do Until tmpFile = "" ' do until empty string
            ReDim Preserve fileList(i) 'set the size of array
            fileList(i) = tmpFile 'put da name in array
            tmpFile = Dir 'get next filename
            i = i + 1 'increment counter
            Loop

            For i = 0 To UBound(fileList )
            tmpFile = fileList(i)
            <offending & offensive code>
            Next i

            actually it's probably clearer, easy to read etc, so better code.

            Thank you again Mickey.
            build

            Comment

            Working...