views:

395

answers:

2

Could you please tell me how to use "pdftk mypdf.pdf dump data | findstr NumberOfPages in powerbuilder run command and save this metadata in a file by using the following code like this:

string ls_runinput, ls_outputfile

ls_outputfile = "c:\test.txt"
ls_runinput = "c:\pdftk\pdftk.exe mypdf.pdf dump_data | findstr NumberOfPages >"+ls_outputfile 
Run(ls_runinput,Minimized!)

li_fileopen = FileOpen(ls_outputfile ,TextMode!, Read!, Shared!)

The problem is that Run command is executed, the file is created, but fileopen return -1 ? Is it maybe that run cannot recognize the "|" character? What should you propose me to write the right code? Iam using powerbuilder 10.5.2 , Thanks very much in advance

+3  A: 

Powerbuilder does not wait for the process called by Run() to complete. The return values of Run() are based solely on whether it successfully called the external process, not on what the external process did next.

This means that pdftk has most likely completed correctly, but you tried to access the output too soon. You'll have to find some way of working out when it has completed. Maybe call it from a batch file that creates another file before it completes, and then in Powerbuilder check for the existance of that file.

Alternatively, you can use a different method of calling your external process. This is an example of calling an external process via Windows Scripting Host:

OleObject wsh

CONSTANT integer MAXIMIZED = 3
CONSTANT integer MINIMIZED = 2
CONSTANT integer NORMAL = 1
CONSTANT integer HIDE = 0
CONSTANT boolean WAIT = TRUE
CONSTANT boolean NOWAIT = FALSE

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "WScript.Shell" )
li_rc = wsh.Run(ls_runinput, HIDE, TRUE)

(code example cribbed from Stuart Dalby's site).

If you still can't get it working, your best bit is to split it out, and verify that you can do a FileOpen on pre-existing file first, then verify externally that the output of the process called by Run() is correct (eventually).

Just for reference, the | character is not a special character and does not need escaping in a string.

Colin Pickard
+1  A: 

Roland Smith has a library and example for doing Run and Wait on his website that may do what you need:

http://www.topwizprogramming.com/freecode_runandwait.html

There are other variations out there that do similar things (we acquired an object called uo_syncproc from somewhere that uses various windows functions to do this (CreateProcessA, WaitForSingleObject, CloseHandle).

Dougman