I managed to set a flag, then loop on exceptions with a "LoadFromFile)"
until there was no exception, and that seems to solve the problem!

Thanks Sid,
Rich


Sid Gudes wrote

>I haven't tried this, but probably what you can do is try to open the
> file exclusively (fmShareExclusive).  If it is in use by DOS, then
> the open should fail.  Once DOS finishes and closes the file, the
> open should succeed.  So something like this:
>
> waiting := 100;
> repeat
>    try
>       fs := tFileStream.create (D+'harbor.txt', fmOpenRead or
> fmShareExclusive);
>       // at this point the open succeeded, so break out of the loop
>       fs.destroy;
>       break;
>    except
>       // we get here if the file was not openable, or does not exist
>       sleep (100);
>    end;
>    dec (waiting);
> until waiting <= 0;
> if waiting <= 0 then error...
>
> The above is OTTOMH.  I think also, in case there is an old version
> of harbor.txt on the hard drive, that it would make sense to delete
> the file before running the DOS command, otherwise the
> tFileStream.create might just succeed on the old file before the DOS
> session gets underway.
>
> BTW, should
>    '/c '+'ipconfig/all > harbor.txt'
>
> be instead
>    '/c '+'ipconfig/all > ' + D + 'harbor.txt'
> ?
>
> HTH
>
>
> At 09:07 AM 7/6/2006, Rich Cooper wrote:
>>Thanks Rob,
>>
>>But actually, I did get one to work.  It finally gelled for the following
>>code in my OnFormCreate handler:
>>
>>   D := GetStartDir;
>>   ShellExecute( 0, 'open', PChar('command.com'),
>>                 PChar('/c '+'ipconfig/all > harbor.txt'),
>>                 nil, SW_HIDE );
>>   Waiting := 100;
>>   while ( (Waiting>0) and (not FileExists(D+'HARBOR.TXT')))
>>   do begin
>>         Sleep(250);
>>         Waiting := Waiting-1;
>>      end;
>>
>>   if   (not FileExists(D+'HARBOR.TXT'))
>>   then raise Exception.Create('Login Error 1.');
>>
>>But there is still a problem.  The 'Waiting' loop discovers that the
>>file exists, but not whether it has been completely written and let
>>loose to be read.  In my OnFormActivate handler, I use:
>>
>>    if   FirstActivation
>>    then begin
>>           D := GetStartDir;
>>           Application.ProcessMessages;
>>           Sleep(500);
>>
>>           if   FileExists(D+'HARBOR.TXT')
>>           then begin
>>                   meHarbor.Lines.LoadFromFile(D+'HARBOR.TXT');
>>
>>which works MOST of the time.  But occasionally, (5%) it causes
>>an exception related to trying to read a file that is still being written.
>>
>>Does anyone have a way to determine whether the file HARBOR.TXT
>>has been released for reading after being completely written?  I would
>>be able to fix this problem by replacing the Sleep(500) with a loop
>>that tests till the HARBOR.TXT file is ready.
>>
>>Thanks,
>>Rich
>>
>>
>>Rob Kennedy wrote
>>
>> > Rich Cooper wrote:
>> >> I'm trying to pipe a DOS command to a file using a ShellExecute,
>> >> but it doesn't create  the output file.  Here's the code:
>> >>
>> >> var D : string;
>> >> ...
>> >>   D := GetStartDir;
>> >>   ShellExecute(0,pChar('ipconfig/all >
>> >> '),pchar('harbor.txt'),nil,pChar(D),SW_SHOWNORMAL);
>> >> ...
>> >>
>> >> but no file named 'harbor.txt' gets created.  Does anyone know how to
>> >> fix this?
>> >
>> > First, "ipconfig/all >" is not a shell verb. Did you read the
>> > documentation for ShellExecute before composing the code above?
>> >
>> > Second, you're telling ShellExecute to look in the registry for the
>> > "ipconfig/all >" key for files named *.txt, and then execute the 
>> > command
>> > it finds there on the harbor.txt file, which I'm guessing doesn't even
>> > exist.
>> >
>> > Third, note that command-line redirection is performed by the
>> > command-line interpreter. ShellExecute is not a command-line
>> > interpreter. Read this:
>> >
>> > http://blogs.msdn.com/oldnewthing/archive/2006/05/16/598893.aspx
>> >
>> > You should be able to find lots of example code to solve your problem
>> > with the following search:
>> >
>> > http://groups.google.com/groups?q=pipe+dos+command&as_ugroup=*delphi*
>> >
>> > Fourth, ipconfig is not a DOS command. It's a console program, but it's
>> > a fully fledged Windows program. Try to run it in DOS (if you even have
>> > a computer with DOS installed anymore), and you'll simply be told that
>> > it needs to run in Win32 mode.
>> >
>> > Finally, type-casting a string literal to PChar is not necessary and 
>> > can
>> > sometimes lead to problems. A string literal can be used as any
>> > string-related type, including AnsiString, WideString, PAnsiChar, and
>> > PWideChar. The compiler will choose based on what it needs. You don't
>> > need to tell it.
>> > --
>> > Rob
>
> Regards,
> Sid Gudes
> PIA Systems Corporation
> [EMAIL PROTECTED]

_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://www.elists.org/mailman/listinfo/delphi

Reply via email to