Thanks Stephen,
I found a web page with a similar snippet, and so I was able to
get the following to work 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 95% of the time. But sometimes it causes the LoadFromFile
procedure to throw an exception related to trying to read a file that is
not yet ready to be read - still locked by the NT file system in XP, and not
yet completely written to the HARBOR.TXT file and then released to
the rest of the computer.
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 for reading, having been
fully released.
Thanks,
Rich
Stephen Posey 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?
>
> The issue with "piping" is that it's a function of the "DOS" command
> processor (CMD.EXE under NT/Win2000/XP); ShellExecute on its own knows
> nothing of it.
>
> In order to accomplish what you're describing you'll need to invoke the
> command processor to run the command. Something like:
>
> ShellExecute(0,
> pchar('OPEN'),
> pchar('c:\windows\system32\cmd.exe'),
> pchar(' /c c:\windows\system32\ipconfig.exe /all > c:\harbor.txt'),
> pchar('c:\'),
> SW_SHOWNORMAL);
>
> ought to do it.
>
> HTH
>
> Stephen Posey
> [EMAIL PROTECTED]
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi