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

When Does a File Actually Exist?

Status
Not open for further replies.

6volt

Programmer
Jun 4, 2003
74
US
OBJECTIVE:

Download a stock datafile using the WGET program and then append to it.
_________________________________________________________
PROBLEM:

Cannot determine when file is ready for appending.
_________________________________________________________
DISCUSSION:

I use ShellExecute to run WGET as follows:

wgetString := PChar ('-O ' +
wgetFileName +
' +
's=' + symbol +
'&a=' + IntToStr (fromMonth) +
'&b=' + IntToStr (fromDay ) +
'&c=' + IntToStr (fromYear ) +
'&d=' + IntToStr ( toMonth) +
'&e=' + IntToStr ( toDay ) +
'&f=' + IntToStr ( toYear ) +
'&g=d' +
'&q=q' +
'&y=0' +
'&z=f' +
'&x=.csv');

errorcode := SHELLexecute ( Form1.Handle,
'open',
PChar('c:\wget.exe'),
wgetString,
nil,
sw_shownormal);

I append using the following simple approach:

AssignFile (wgetFile, wgetFileName );
Append (wgetFile);

WHILE NOT Eof (dataFile) DO
BEGIN
ReadLn (dataFile, nextLine);
WriteLn (wgetFile, nextLine);
END;

Methods tried to determine if the file, wgetFileName, exists:

1) Test for assignment of ShellExecute errorcode.
2) Test for FileExists (wgetFileName) = TRUE.

Both methods are satisfied BEFORE wgetFileName is ready for appending.

My guess is that the file exists while it is being written to. So what test do you use to determine that writing is complete?

Of course that is only a guess.

Thanks in advance.
Tom
 
Tom,

Just a wild idea, but I've noticed that Explorer reports a file size of zero while files are being downloaded. Perhaps you could check the file size and wait for it to be non-zero.

Again, it's just a WAG; I haven't actually tested the idea.

-- Lance
 
I've just been fooling around with FileSize, however, this can only be used after Assign.

Perhaps, I have to go to some Windows commands to get this information rather than from within Delphi.

Thanks
Tom
 
Perhaps if you try and open it exclusively, that will fail until the download is complete:
Code:
    DropDeadTime := Now + (10 / (24 * 60 * 60));   // ten seconds

    while Now < DropDeadTime do
    begin
        try
            with TFileStream.Create(AFileName, fmOpenRead + fmShareExclusive) do
            try
                SetLength(Buffer, Size);
                Read(PChar(Buffer)^, Size);
                ShowMessage(Buffer);
            finally
                Free;
            end;
            Break;
        except
            on EFOpenError do
                ;
        end;
    end;

Cheers
 
...lots like I have to learn about FileStreams. (I assume they are good for TextFiles?)
 
TFileStream is a wrapper around raw file access. &quot;Text Files&quot; and &quot;Record Files&quot; are Pascal artifacts (e.g. deprecated).

You are writing raw bytes to the file: it's your responsibility to make sure they are ASCII bytes or whatever.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top