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

ShellExecute tp print and then delete file 1

Status
Not open for further replies.

michaelkatz

Programmer
Sep 1, 2002
50
GB
I am using ShellExecute to print a PDF file. I gave up on trying to close adobe Acrobat when the printing is done.

I also want to delete the file after it is printed. However, the file is in use until released by ShellExecute and the Delete command fails, or the delete command executes too quickly and ShellExecute cannot find the file. I tried using the following code:

DO WHILE FILE(cFile) = .T.
DELETE FILE cFile
enddo

Problem: Takes very long. Appears The Delete command and ShellExecute are fighting for processor time.

I tried using a progress bar.=, but it doesn't solve anything.

The only thing that seems to work is:

Wait Window Timeout nSeconds

...while the user stares at a apparently frozen screen.

Thanks,
Michael
 
Hi

VFP does not wait for the command to be completed when done thru ShellExecute. And that is the reason for your problem. An alternative will be..

LOCAL lcCmd, loShell
lcCmd = GETENV("ComSpec") + [ /C ] + myCommand
loShell = CREATEOBJECT("wscript.shell")
= loShell.Run(lcCmd,0,.t.)
* 0 to hide display, .t. to wait for completion

Now in the above you can suitably use myCommand and achieve your work. For example.. myCommand can be just a batch file and the batch file run. To create a batch file, save it .. as given below for example..

Text TO cString
myBatch Command Line 1
myBatch Command Line 2
myBatch Command Line 3
.. etc...
EndText
=STRTOFILE(cFileName,cString)
And now you can run this Batch file using above loShell.

This way you can achieve what you want and make VFP wait for the completion of the commands.

Another way is to always copy the file you want to print in a specific printspool directory of your own using a
SYS(3) or such variable name. Dont wait to delete the temporary file after shell executing the print. So you can proceed with the application without having to wait for the printer to even complete the job. But the way to keep the house clean is that whenever you start application or such output routines, simply try to ERASE *.* of the spool directory so kept. Whatever is in progress, will not get deleted by OS lock. However, they will go out when the next such access is done. The spool directory will only have these files for a scession which should not be a cause to worry.

Hope these idea get you going :)

____________________________________________
ramani - (Subramanian.G) :)
 
Thanks Ramani,

I haven't tried the first suggestion yet, but the second is brilliant. I'm ashamed I didn't think of it myself.

Michael
 
Hi Michael

when you adapt the spool directory method and want to erase all files in the spool directory on next startup..

If the file is still under printing, you may get an error. the way to catch the error is..


** This will avoid the error message of still processed files obstructing the erase command.

myErrorHandler = ON('ERROR')
mySafety = SET('SAFETY')

ON ERROR doNothing=''
SET SAFETY OFF

ERASE c:\mySpool\*.*

IF !EMPTY(myErrorHandler)
ON ERROR &myErrorHandler
ELSE
ON ERROR
ENDIF
SET SAFETY &mySafety

The idea is to avoid the default system error message box pop up.

Just thought will provide the complete idea so that any one going thru the thread will get a complete picture.

:)

____________________________________________
ramani - (Subramanian.G) :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top