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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with ADIR() function

Status
Not open for further replies.

IRABYY

Programmer
Apr 18, 2002
221
US
Colleagues:

Have anyone ever encountered the following:

ADIR(SomeArray, SomePath, "D") returns number of rows greater than the array actually holds - or so it seems?

I have the following function in my program to determine if the given directory is empty:
Code:
******************************************************************************************************************************
FUNCTION IsEmptyDir(tcDir)
******************************************************************************************************************************
** Function Name : Is Empty Dir
** Purpose       : Verifies if the given directory is empty.
** Description   : Checks presence of any files and/or subdirectories in the given directory by running ADIR() and checking
**                 the contents of the resulting array. If there's anything in the first cell in any array's row different
**                 from "." and ".." - directory is not empty.
** Parameter(s)  : Directory in ? as string.
** Return        : "Empty" flag as Boolean.
** Side Effect(s): None.
** Notes:        : 1. Silent function.
**                 2. It's assumed that the path/dir existence has been already verified by the calling subroutine.
******************************************************************************************************************************
LOCAL lnMax, I, llRet
LOCAL ARRAY laDir[1]

llRet = .T.  && Initially
tcDir = ADDBS(ALLTRIM(tcDir))
lnMax = ADIR(laDir, tcDir + "*.*", "D")
FOR I = 1 TO lnMax
   IF !(laDir[I, 1] == "." .OR. laDir[I, 1] == "..") && Skipping reference to the tcDir itself
      llRet = .F.
      EXIT  && FOR...NEXT CYCLE 
   ENDIF
NEXT I

RETURN llRet
ENDFUNC
******************************************************************************************************************************

Now, this thing has been working perfectly on all directories at all customers' - until today: one customer calls and says he's getting an error #31, "Invalid subscript reference" from this function. He tried it on another directory - no problem.

Hard as I tried, I could not reproduce this behavior.

I even created the same directory on my machine. I even made some or all files in that directory invisible - every time this function returned correct result whether I added "H" clause to the 3rd parameter (counted hidden files) or not (skipped the hidden files).

Any clue?

Thanks!


Regards,

Ilya
 
Ilya,

Look to my thread - 'Problem with ADIR() function'. I'm beginning to suspect that something's changed because of a Windows update/patch. Have any patches been applied to the system your application is running on?

Steve
 
Sorry, bro, could not find 'Problem with ADIR() function'thread you've mentioned. If it's yours - could you post the link to it, please?

Thanks!


Regards,

Ilya
 
Ilya,

Sorry about that -- The caption is "Element Order in ADIR()". I haven't been able to figure out how to include links to other threads or I'd do so...

Steve
 
SGLong (Programmer):

The caption is "Element Order in ADIR()".

Yes, I saw it. I do not think it has something to do with the problem I've run across to.

I even searched MSDN/Knowledge Base (ADIR() and 31) - found nothing.

Can that be that lnMax = ADIR(laDir, tcDir + "*.*", "D") line is the "culprit"? Is there any limit to the number of files + directories ADIR() is capable of putting into array, considering that array is limited to 65,000 elements?
OTOH, the number of the files in a directory is also limited to 20 thousand + "change"... that's 100,000+ elements to put into array. I will have to check it also.

Later!



Regards,

Ilya
 
IRABYY

May I suggest a different approach.

Code:
Local lnMax, llRet
Local Array laDir[1]
llRet = .T.  && Initially
tcDir = Addbs(Alltrim(tcDir))
lnMax = Adir(laDir,'*.*', "D")
nPos = Ascan(laDir,".")
If nPos # 0
	= Adel(laDir, nPos)
	lnMax = lnMax - 1
Endif
nPos = Ascan(laDir,"..")
If nPos # 0
	= Adel(laDir, nPos)
	lnMax = lnMax - 1
Endif


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
You're running into one of VFP's limitations.

VFP has an array element limit of 65000. Since ADIR() returns a 5 column array (5 elements per file), this means that if the directory in question has 13000 or more entries, that limit will be exceeded. The the error "Invalid Subscript Reference" occurs on the ADIR() command itself, unfortunately.

I suggest you trap the error and ignore it. The ADIR() command will still create the array and it will be filled with the first few thousand files. Your function will still serve its purpose.

Ian
 
Colleagues,

Ian and I were right suspecting ADIR() caused that error. As it turned out, customer indeed had 13,500+ files in this directory, although on our mirror image of that directory in our shop there were less than 140 files. Why customer had so outrageous number of files - that's another story to tell.
My program merges new data to existing data (the latter resides on a network drive), for the data retrieving by end users with another (also mine) program. In essence, this merge process runs like this:
Code:
USE MasterTable ALIAS MASTER IN 0 EXCLUSIVE
USE CurrentTable ALIAS CURRENT IN 0
SELECT CURRENT
FOR I = 1 TO RECCOUNT("CURRENT")
   SCATTER MEMVAR
   INSERT INTO (DBF("MASTER")) FROM MEMVAR
   FLUSH
NEXT I
Sure thing, the Master file to be opened for exclusive use, all the end users should be given a warning ahead of time to exit the data retrieving program. But Sys. Admin used to solve this issue very simple: he just disconnected all the users off this network HDD! Of course, abnormal termination; of course, all the temporary cursors' files (*.TMP) remained on disk - hence 13,300+ extra files (all of them TMP) in that Master Merge directory. When I explained all this to the guy - that was like revelation for him, he-he! Well, now he knows better! And I, on my part, added DELETE FILE *.TMP before anything in my code.

Thanks everybody, I appreciate your time and help!

The issue is resolved, case is being closed.


Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top