Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through results of DIR() function?

Status
Not open for further replies.

kenndot

Programmer
May 15, 2001
316
US
I just want to loop through the results of the DIR() function, but I don't know the most efficient way to do this. Should I use filetostr() and put the results to a string? When I do this I get all kinds of tabs and spaces to sift through and it seemed like a lot of trouble for what I'm trying to accomplish and I figured I'd ask here to see if there was an easier way before I took the time to parse everything out. Anyone done this before and have a quick solution?

Thanks
 
thanks, i knew there was something better!
 
With the ADIR() function, you just have to be careful that you don't use it in such a way that you'll get more than 13000 files in the resulting array - or you'll get an error.

ADIR() returns an array, and arrays are limited to 65000 elements. Since the array has 5 columns (at least in VFP 7), you are limited to 65000/5 rows = 13000 files before the code will blow up.

You could use the SYS(2000) function to do wildcard searches.

But lately, I've found myself using Windows Scripting Host objects. If you get a Folder object from a FileSystemObject, then loop through the Files collection in the Folder object, you can do all sorts of interesting things, since the file object has lots of properties/methods.

Here's a sample of what I'm talking about:

loFS=CREATEOBJECT("scripting.filesystemobject")
lcFolder = GETDIR()
IF DIRECTORY(lcFolder)
loFolder = loFS.GetFolder(lcFolder)
FOR EACH loFile IN loFolder.Files
IF UPPER(JUSTEXT(loFile.Name)) == "PRG"
* Here is a .PRG file in the folder.
?loFile.Name
ENDIF
ENDFOR
ENDIF

I've encountered strange OLE errors trying to access the Files collection of the Folder directly (passing any sort of index), but the FOR EACH loop seems to work fine, and it gives you a quick and easy way to use the file objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top