Just starting out? Need help? Post your questions and find answers here.
zikitrake
Addict
Posts: 834 Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain
Post
by zikitrake » Thu May 23, 2019 2:53 pm
(
PB 5.70x64, Windows 10 x64 )
Hi, I'm trying to get WGET for Windows Output (From here:
https://eternallybored.org/misc/wget/releases/ , 64 bits version)
I have this code, but I don't know what to do to make AvailableProgramOutput() work properly; It always return 0, so the output is always
My code:
Code: Select all
Procedure.S WgetMain()
Protected Program.s, parameters.s, Error.s, Handle, Thread, ExitCode, Size, *Buffer
Program = "wget.exe"
parameters = " -r https://www.demos.com/ -nd -P wgetTMP"
hProgram = RunProgram("wget.exe",parameters,"",#PB_Program_Open|#PB_Program_Read|#PB_Program_Hide|#PB_Program_Error)
If hProgram And IsProgram(hProgram)
While ProgramRunning(hProgram)
If AvailableProgramOutput(hProgram)
lTmp$=ReadProgramString(hProgram)
If Len(lTmp$)>0
Debug "wget-Ok:> "+lTmp$
EndIf
EndIf
lTmp$ = ReadProgramError(hProgram)
If Len(lTmp$)>0
Debug "wget-Error:> "+lTmp$
EndIf
Delay(10)
Wend
CloseProgram(hProgram) ; Close the connection to the program
Else
Debug "Not a valid program handle"
EndIf
EndProcedure
WgetMain()
End
Does anyone have any idea why I can't read the output correctly?
Thank you!
Last edited by
zikitrake on Thu May 23, 2019 4:58 pm, edited 1 time in total.
infratec
Always Here
Posts: 6874 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu May 23, 2019 3:44 pm
Use -o - as additional parameter
Code: Select all
parameters = " -r https://www.demos.com/ -nd -P wgetTMP -o -"
ebs
Enthusiast
Posts: 530 Joined: Fri Apr 25, 2003 11:08 pm
Post
by ebs » Thu May 23, 2019 3:52 pm
I found that "wget.exe" writes its output to stderr, not stdout, so I used
ReadProgramError() instead of
ReadProgramString() , like this:
Code: Select all
Program.L = RunProgram("wget.exe", host + Path, ".", #PB_Program_Open|#PB_Program_Error|#PB_Program_Hide)
; display progress
If Program
While ProgramRunning(Program)
Delay(1)
If ProgressBar
OS.S = ReadProgramError(Program)
If OS
Pos.L = FindString(OS, "%")
If Pos
Percent.L = Val(Mid(OS, Pos - 3))
; update progress bar
If Percent > PrevValue.L
SetGadgetState(ProgressBar, Percent)
PrevValue = Percent
DoEvents()
EndIf
EndIf
EndIf
EndIf
Wend
infratec
Always Here
Posts: 6874 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu May 23, 2019 3:58 pm
And you have to remove the backspaces
Code: Select all
Procedure.S WgetMain()
Protected Program.s, parameters.s, Error.s, Handle, Thread, ExitCode, Size, *Buffer
Program = "wget.exe"
parameters = " -r https://www.demos.com/ -nd -P wgetTMP -o -"
*Buffer = AllocateMemory(2048)
hProgram = RunProgram("wget.exe",parameters,"",#PB_Program_Open|#PB_Program_Read|#PB_Program_Hide|#PB_Program_Error)
If hProgram And IsProgram(hProgram)
While ProgramRunning(hProgram)
Size = AvailableProgramOutput(hProgram)
If Size
Size = ReadProgramData(hProgram, *Buffer, Size)
If Size
String$ = PeekS(*Buffer, Size, #PB_UTF8|#PB_ByteLength)
String$ = RemoveString(String$, #BS$)
Result$ + String$
;Debug "wget-Ok:> "+ String$
EndIf
EndIf
lTmp$ = ReadProgramError(hProgram)
If lTmp$ <> ""
Debug "wget-Error:> "+lTmp$
EndIf
Delay(1)
Wend
CloseProgram(hProgram) ; Close the connection to the program
Debug Result$
Else
Debug "Not a valid program handle"
EndIf
EndProcedure
WgetMain()
End
zikitrake
Addict
Posts: 834 Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain
Post
by zikitrake » Thu May 23, 2019 4:38 pm
infratec wrote: And you have to remove the backspaces
Thank you, It's working fine now
ebs wrote: I found that "wget.exe" writes its output to stderr, not stdout, so I used ReadProgramError() instead of ReadProgramString() , like this...
Thanks! I was thinking of using a similar code, but the code passed by infratec seems more 'adequate'.
Thank you both!