RE: similiar to opening a csv file in default program

2006-07-10 Thread Jeff Young
Welcome. Just wish I could find you the good version of that, but it seems
to have disappeared from my archives. The good version of it used named
pipes instead of a disk file. I resorted to named pipes in the end because 
GetTempPath would fail on some (10%) of the server systems I encountered and
the path had to be hardwired in. Hopefully that will work for you.

Lovely!  Thanks, Jeff
-Rich



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


Re: similiar to opening a csv file in default program

2006-07-10 Thread Stephen Posey
Rich Cooper wrote:
snipz
 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.

 I don't know precisely how cmd.exe creates and opens a redirected file
 like that, but your attempting to open it in exclusive share mode
 ought to fail regardless if the file is still being written.

 You can use a TFileStream instance, like so:

   FS := TFileStream.Create(D + 'HARBOR.TXT',
fmOpenRead or fmShareExclusive);

 Attempt that and catch any exception raised due to the sharing conflict
 (should be some form of EInOutError), if the file exists and no
 exception is raised on attempting to open it, then it should be finished.

 HTH

 Stephen Posey
 [EMAIL PROTECTED]
 
 I tried LoadFromFile() with try ... except end around it and kept
 looping till the exceptions went away - that worked just fine!
 
 Thanks Stephen and Simon,

Glad you got it working.

Stephen Posey
[EMAIL PROTECTED]

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


Re: similiar to opening a csv file in default program

2006-07-09 Thread Rich Cooper
Stephen Posey wrote:

 Rich Cooper wrote:
 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 ( (Waiting0) 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.

 I don't know precisely how cmd.exe creates and opens a redirected file
 like that, but your attempting to open it in exclusive share mode
 ought to fail regardless if the file is still being written.

 You can use a TFileStream instance, like so:

   FS := TFileStream.Create(D + 'HARBOR.TXT',
fmOpenRead or fmShareExclusive);

 Attempt that and catch any exception raised due to the sharing conflict
 (should be some form of EInOutError), if the file exists and no
 exception is raised on attempting to open it, then it should be finished.

 HTH

 Stephen Posey
 [EMAIL PROTECTED]

I tried LoadFromFile() with try ... except end around it and kept
looping till the exceptions went away - that worked just fine!

Thanks Stephen and Simon,
Rich 

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


Re: similiar to opening a csv file in default program

2006-07-09 Thread Rich Cooper
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 ( (Waiting0) 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+commandas_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


Re: similiar to opening a csv file in default program

2006-07-09 Thread Rich Cooper
Lovely!  Thanks, Jeff
-Rich

Jeff Young wrote

 Rich,
 Thought I'd chime in with code that I've used for the very same
 process. It worked on D7... HTH.
 
 
 unit Process;
 
 interface
 
 uses
 Classes, Windows, SysUtils;
 
 function ExecuteWait(Path, Command, Params: String; ShowWindow: Word;
 Output: TStringList): DWord;
 function GetTempFile: String;
 
 implementation
 
 function ExecuteWait(Path, Command, Params: String; ShowWindow: Word;
 Output: TStringList): DWord;
 // Build a temporary filename ---
 var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  saAttr: TSecurityAttributes;
  hOut, hInp: THandle;
  outFile, inpFile: String;
 
 begin
  if Path = '' then begin
Output.Add('Path not specified: ' + Path);
exit;
  end;// if Path
  saAttr.nLength := sizeof(TSecurityAttributes);
  saAttr.bInheritHandle := True;
  saAttr.lpSecurityDescriptor := nil;
  hOut := STD_OUTPUT_HANDLE;
  hInp := STD_INPUT_HANDLE;
 
  if Output  nil then begin
outFile := GetTempFile;
hOut := CreateFile(PChar(outFile),
 
 GENERIC_READ or GENERIC_WRITE,
   0,
   @saAttr,
   CREATE_ALWAYS,
   FILE_ATTRIBUTE_TEMPORARY,
   0);
   end; // if Output  nil
 
  ZeroMemory(@ProcessInfo, SizeOf(TProcessInformation));
  ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
 
  with StartupInfo do begin
cb  := SizeOf(TStartupInfo);
hStdOutput  := hOut;
hStdError   := hOut;
hStdInput   := hInp;
wShowWindow := ShowWindow;
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  end; // with
 
  if CreateProcess(nil,
  PChar(''+
 Path + Command + ' ' + Params),
   @saAttr,
   @saAttr,
   True,
   0,
   nil,
   PChar(Path),
   StartupInfo,
   ProcessInfo) then begin
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
repeat
  GetExitCodeProcess(ProcessInfo.hProcess, Result);
  //Application.ProcessMessages;
until (Result  STILL_ACTIVE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
if Output  nil then begin
  CloseHandle(hOut);
  Output.LoadFromFile(outFile);
 end; // if Output  nil
DeleteFile(inpFile);
DeleteFile(outFile);
   end // if CreateProcess(...
  else
Output.Add('Create Process failed. Code: ' +
  IntToStr(GetLastError()));
 end; //
 
 function GetTempFile: String;
 var
  DirBuf, FileBuf: Array [0..255] of char;
 begin
  GetTempPath(Length(DirBuf), DirBuf);
  GetTempFileName(DirBuf, 'tmp', 0, PChar(@FileBuf));
  Result := FileBuf;
 end;//GetTempFile
 
 end.
 
 
 ___
 Delphi mailing list - Delphi@elists.org
 http://www.elists.org/mailman/listinfo/delphi

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


Re: similiar to opening a csv file in default program

2006-07-08 Thread Stephen Posey
Rich Cooper wrote:
 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 ( (Waiting0) 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.

I don't know precisely how cmd.exe creates and opens a redirected file 
like that, but your attempting to open it in exclusive share mode 
ought to fail regardless if the file is still being written.

You can use a TFileStream instance, like so:

   FS := TFileStream.Create(D + 'HARBOR.TXT',
fmOpenRead or fmShareExclusive);

Attempt that and catch any exception raised due to the sharing conflict 
(should be some form of EInOutError), if the file exists and no 
exception is raised on attempting to open it, then it should be finished.

HTH

Stephen Posey
[EMAIL PROTECTED]

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


Re: similiar to opening a csv file in default program

2006-07-08 Thread Sid Gudes
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 ( (Waiting0) 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+commandas_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


Re: similiar to opening a csv file in default program

2006-07-06 Thread Rich Cooper
Thanks Wim!  I finally got it figured out.

-Rich

Wim Sterns 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
 Rich,
 
 try
 pchar('d:\harbor.txt'),nil,pchar('d:\')
 
 
 Wim
 
 
 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?
 
 Thanks,
 Rich

 
 __
 Do You Yahoo!?
 Tired of spam?  Yahoo! Mail has the best spam protection around 
 http://mail.yahoo.com 
 ___
 Delphi mailing list - Delphi@elists.org
 http://www.elists.org/mailman/listinfo/delphi

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


Re: similiar to opening a csv file in default program

2006-07-06 Thread Stephen Posey
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]




This message and its attachments are for the sole use of the intended 
recipient(s) and may contain confidential, 
 proprietary and privileged information. Any unauthorized review, copying, use, 
disclosure or distribution is prohibited. If 
 you are not the intended recipient, please notify the sender immediately by 
replying to the address listed in the From: 
 field and destroy all copies of the original message and its attachments.
___
Delphi mailing list - Delphi@elists.org
http://www.elists.org/mailman/listinfo/delphi


Re: similiar to opening a csv file in default program

2006-07-06 Thread Rich Cooper
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 ( (Waiting0) 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+commandas_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
 ___
 Delphi mailing list - Delphi@elists.org
 http://www.elists.org/mailman/listinfo/delphi

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


Re: similiar to opening a csv file in default program

2006-07-06 Thread Rich Cooper
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 ( (Waiting0) 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 - Delphi@elists.org
http://www.elists.org/mailman/listinfo/delphi


Re: similiar to opening a csv file in default program

2006-07-05 Thread Rich Cooper
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?

Thanks,
Rich

Brendan Blake wrote:

 ShellExecute works fine for me:  (still on Delphi 5)

 ShellExecute(0,'open',PChar(filen),nil,nil,sw_ShowNormal);

 Where filen is a string containing the full path, filename and file
 type/extension (.csv)   You need uses ShellAPI (I recall).

 My default application is Excel. Works fine. Maybe you have not got the
 registration correctly set up to your default application?

 Regards,
   Brendan.

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
 Behalf Of Chris Stebbing
 Sent: 03 July 2006 09:37
 To: Delphi List
 Subject: opening a csv file in default program

 Hi All,

 I'd like to be able to launch a CSV file into Excel or whatever
 happens to be the default application to handle CSV's.  I had though
 that I could just do a ShellEx or CreateProcess with the csv as the
 application name, but that doesn't seem to work.

 Can anyone advise how I might acheve this?

 Ta muchly,
 Chris.

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

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

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