wget.exe AvailableProgramOutput() Error [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
zikitrake
Addict
Addict
Posts: 834
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

wget.exe AvailableProgramOutput() Error [SOLVED]

Post by zikitrake »

(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
wget-Error:> --2019-05-23 16:04:46-- https://www.demos.com/
wget-Error:> Resolving http://www.demos.com (http://www.demos.com)... 54.85.128.122, 34.200.198.230
wget-Error:> Connecting to http://www.demos.com (http://www.demos.com)|54.85.128.122|:443... connected.
wget-Error:> HTTP request sent, awaiting response... 200 OK
...
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
Always Here
Posts: 6824
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: wget.exe AvailableProgramOutput() Error

Post by infratec »

Use -o - as additional parameter

Code: Select all

parameters =  " -r https://www.demos.com/ -nd -P wgetTMP -o -"
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: wget.exe AvailableProgramOutput() Error

Post by ebs »

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
Always Here
Posts: 6824
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: wget.exe AvailableProgramOutput() Error

Post by infratec »

And you have to remove the backspaces :wink:

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
Addict
Posts: 834
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: wget.exe AvailableProgramOutput() Error

Post by zikitrake »

infratec wrote:And you have to remove the backspaces :wink:

Code: Select all

Procedure.S WgetMain()....
:shock: 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!
Post Reply