By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Dir() in Loop returns ""

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 21 Sep 2004 20:06:38 +1000, build <bu*****@datafast.net.au>
wrote:
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.


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
Jul 17 '05 #2

P: n/a
build wrote:
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

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
Jul 17 '05 #3

P: n/a
> > 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 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?


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.
Is that clearer?


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

Rick - MVP

Jul 17 '05 #4

P: n/a
On Thu, 23 Sep 2004 07:22:23 +1000, build <bu*****@datafast.net.au>
you typed some letters in random order:
build wrote:
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

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


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(Pad & "\" & 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 ####
Jul 17 '05 #5

P: n/a
mickey wrote:
On Thu, 23 Sep 2004 07:22:23 +1000, build <bu*****@datafast.net.au>
you typed some letters in random order:

build wrote:
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


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

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(Pad & "\" & MyFile) < Datum Then
Kill (Pad & "\" & MyFile)
End If
MyFile = Dir ' Get next entry.
Loop
End Function

</code>

Groetjenz,

Mickey


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
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.