I can't make this dir loop work

  • Thread starter ragtopcaddy via AccessMonster.com
  • Start date
R

ragtopcaddy via AccessMonster.com

Here's my code:

strFileName = Dir(strPath & "*.xls")
Do While strFileName <> vbNullString
strFullPath = strPath & strFileName
'Do stuff here
strFileName = Dir
Loop

The problem is, the files are skipping around. The filename goes from "0707"
to "0709" and skips "0708". any ideas?
 
D

Douglas J. Steele

The Dir function isn't guaranteed to return the files in any specific order.

When you say it's skipping 0708, does it eventually find it if you keep the
code running?
 
R

ragtopcaddy via AccessMonster.com

Douglas,

I'll soon find out!

Thanks for your reply,

The Dir function isn't guaranteed to return the files in any specific order.

When you say it's skipping 0708, does it eventually find it if you keep the
code running?
Here's my code:
[quoted text clipped - 8 lines]
"0707"
to "0709" and skips "0708". any ideas?
 
R

ragtopcaddy via AccessMonster.com

Doug,

No dice! There are 12 files in the directory, but even with my manually
resetting strFileName on the 1st couple of passes, I only got 6 of them.
Could you please give me a code example of how to do this?

Thanks,

The Dir function isn't guaranteed to return the files in any specific order.

When you say it's skipping 0708, does it eventually find it if you keep the
code running?
Here's my code:
[quoted text clipped - 8 lines]
"0707"
to "0709" and skips "0708". any ideas?
 
D

Douglas J. Steele

Your code snippet should work: there's no other code that I could give you!

The only change I'd make is to replace

Do While strFileName <> vbNullString

with

Do While Len(strFileName) > 0

Exactly what are you doing in the

' Do stuff here

section?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


ragtopcaddy via AccessMonster.com said:
Doug,

No dice! There are 12 files in the directory, but even with my manually
resetting strFileName on the 1st couple of passes, I only got 6 of them.
Could you please give me a code example of how to do this?

Thanks,

The Dir function isn't guaranteed to return the files in any specific
order.

When you say it's skipping 0708, does it eventually find it if you keep
the
code running?
Here's my code:
[quoted text clipped - 8 lines]
"0707"
to "0709" and skips "0708". any ideas?
 
R

ragtopcaddy via AccessMonster.com

Doug,

I can't for the life of me see how this code could possibly work.

There is no equivalent to ".MoveNext" or "Next strFileName" or anything.

How could it possibly loop through a list of 12 files when there is no
apparent mechanism to do so?

Thanks,


Your code snippet should work: there's no other code that I could give you!

The only change I'd make is to replace

Do While strFileName <> vbNullString

with

Do While Len(strFileName) > 0

Exactly what are you doing in the

' Do stuff here

section?
[quoted text clipped - 16 lines]
 
D

Douglas J. Steele

From the Help file:

"Dir returns the first file name that matches pathname. To get any
additional file names that match pathname, call Dir again with no arguments.
When no more file names match, Dir returns a zero-length string (""). Once a
zero-length string is returned, you must specify pathname in subsequent
calls or an error occurs."

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


ragtopcaddy via AccessMonster.com said:
Doug,

I can't for the life of me see how this code could possibly work.

There is no equivalent to ".MoveNext" or "Next strFileName" or anything.

How could it possibly loop through a list of 12 files when there is no
apparent mechanism to do so?

Thanks,


Your code snippet should work: there's no other code that I could give
you!

The only change I'd make is to replace

Do While strFileName <> vbNullString

with

Do While Len(strFileName) > 0

Exactly what are you doing in the

' Do stuff here

section?
[quoted text clipped - 16 lines]
"0707"
to "0709" and skips "0708". any ideas?
 
I

ileana

ragtopcaddy via AccessMonster.com said:
Here's my code:

strFileName = Dir(strPath & "*.xls")
Do While strFileName <> vbNullString
strFullPath = strPath & strFileName
'Do stuff here
strFileName = Dir
Loop

The problem is, the files are skipping around. The filename goes from "0707"
to "0709" and skips "0708". any ideas?
 
Top