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!

Using ADIR() and FILE() 3

Status
Not open for further replies.

ToddWW

Programmer
Mar 25, 2001
1,073
US
I have a customer transferring XML files into an FTP server. I have written a program to determine if any files exist, and if they do, the they are processed.

Currently I am using:
Code:
_fileCount = ADIR(_fileArray,'*.xml')
IF _fileCount > 0
  FOR i = 1 TO _fileCount
    
    _xmlData = FILETOSTR(_fileArray(i,1))
    
    && SOME VALIDATION CODE ON THE _xmlData string.
    
    XMLTOCURSOR(_xmlData)
    
    && PROCESS DATA FROM CURSOR
  
NEXT
ENDIF

The program works great. Then I began to think. What would this program do if a file was in the middle of a transfer to the FTP server.

Any thoughts. Would the ADIR() method recognize the file before the transfer was finished ?

Thanks for your suggestions.

ToddWW
 
I believe the answer to your question is likely to be "yes" ADIR will recognize it even if it has not been written out completely. Windows Explorer behaves the same way.

However, you can check the 2nd parameter to ensure the file size is greater than 0k.

gnnumber = ADIR(myArray, '*.*')
MESSAGEBOX(myArray(1,2))
MESSAGEBOX(myArray(1,1))

The filesize will remain 0 until the new file is closed.



Jim Osieczonek
Delta Business Group, LLC
 
Well, thank you !! That makes perfect sense and should resolve any conflicts down the road.

ToddWW
 
Todd,

Checking the file size for non-zero may not really give you the information you're looking for. I've run into incomplete file transfers doing that. Your best bet is to try to open the file exclusive before transferring it. If you can't then the file is still being built. Two methods I've used are with the FOPEN and RENAME commands, both of which require error trapping of some sort. For the RENAME, the function should try to rename the file to itself (i.e. RENAME myfile.xml TO myfile.XML).

Steve
 
Todd

Following Steve's suggestion, the following code also has a user definable timeout
Code:
[COLOR=blue]lnSeconds = SECONDS()	
DO WHILE .T. [/color][COLOR=green]&& Wait for activity to complete[/color][COLOR=blue]
[tab]IF SECONDS() > lnSeconds + USER.timeout
[tab][tab]llTimedOut = .T.
[tab][tab]EXIT
[tab]ENDI	
[tab]lnOpen = FOPEN(lcFileName,0)
[tab]IF lnOpen = -1 [/color][COLOR=green]&& Check for error opening file[/color][COLOR=blue]
[tab][tab]FCLOSE(lnOpen) [/color][COLOR=green]&& Close file[/color][COLOR=blue]
[tab]ELSE
[tab][tab]FCLOSE(lnOpen) [/color][COLOR=green]&& Close file[/color][COLOR=blue]
[tab][tab]EXIT [/color][COLOR=green]&& Exit loop[/color][COLOR=blue]
[tab]ENDIF
ENDDO[/color]


FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommandertm.com
PDFcommandertm.co.uk


 
Thank you. That was also very helpful. I appreciate all your help.

ToddWW
 
Something you may also want to consider, though it may be unlikely if you have a dependable FTP server/client, is an interrupted transfer. You may end up processing an incomplete file.
It may be advantageous to create some sort of flag file and the end of a successful transfer and have your routine check for those instead. If it finds say, MyXMLFile.dun, have it grab the filename portion, add the extension and process the correct file MyXMLFile.XML.

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Yeah. Right now I'm doing this.
Code:
_xmlData = ALLTRIM(FILETOSTR(_fileName))
IF AT('</Document>',_xmlData) = 0
    Sleep(5000) && Pause 5 Seconds
    _xmlData = ALLTRIM(FILETOSTR(_fileName))
ENDIF
IF AT('</Document>',_xmlData) > 0
    && Process XML
ELSE
    && Error Processing
ENDIF
And I am going to replace it with this.
Code:
_processFile = .T.
_fileHandle = FOPEN(_fileName,0)
IF _fileHandle = -1 && Check for error opening file
    _processFile = .F.    
ENDIF
=FCLOSE(_fileHandle)
IF _processFile
    _xmlData = FILETOSTR(_fileName)
    IF AT('</Document>',_xmlData) > 0
        && Process XML
    ELSE
        && Error Processing
        && Must have been an incomplete FTP Transfer
    ENDIF
ELSE
    && Skip this file.  Try again later.
ENDIF
That should do the trick.

Thanks again guys.

ToddWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top