Saving a changing output to a text file... and update the file each time output changes!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Saving a changing output to a text file... and update the file each time output changes!

#1 Post by Kvc » 09 Sep 2017 21:31

Talking about the wget.exe - which allows to download the stuff from command-line interface. it displays the current download position / value on console window. But, I want to read it via a file without displaying it (can use -q, but it won't give me current download position)

Can anyone help?

wget link:
http://gnuwin32.sourceforge.net/packages/wget.htm

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#2 Post by aGerman » 10 Sep 2017 07:47

Did you already redirect the output to a text file and did you observe the content using a HEX editor? I assume that the percentage wil be updated using backspace and/or carriage return characters.
You could put such a text file in a ZIP archive and upload it here.

Steffen

Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Re: Saving a changing output to a text file... and update the file each time output changes!

#3 Post by Kvc » 11 Sep 2017 19:25

aGerman wrote:Did you already redirect the output to a text file and did you observe the content using a HEX editor?


I tried to simply redirect the Command's output in a simple text file as:

Code: Select all

Wget "link" >a.txt


But, the problem is that... it is still showing the output on the console with all those %ages and the file a.txt is of 0 bytes - even afte the process completes. No update at all.

kvc

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#4 Post by aGerman » 12 Sep 2017 01:19

Sounds like Wget writes to the StdErr stream.
Try...

Code: Select all

Wget "link" >"a.txt" 2>&1
...where StdOut and StdErr are merged and redirected to the file. If you only need to redirect the StdErr use ...

Code: Select all

Wget "link" 2>"a.txt"
...instead.

Steffen

Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Re: Saving a changing output to a text file... and update the file each time output changes!

#5 Post by Kvc » 13 Sep 2017 08:34

aGerman wrote:Sounds like Wget writes to the StdErr stream.


Wow, Amazing! After applying this change i've got the whole log of the download process in the text file. Really great help. :)

Code: Select all

--2017-09-13 19:58:57--  http://download1495.mediafireuserdownload.com/1kx7aj06qbjg/uzyj5151a66gne9/Batch-Store+v.1.0+By+Kvc.zip
Resolving download1495.mediafireuserdownload.com (download1495.mediafireuserdownload.com)... 205.196.123.183
Connecting to download1495.mediafireuserdownload.com (download1495.mediafireuserdownload.com)|205.196.123.183|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8137333 (7.8M) [application/zip]
Saving to: 'Batch-Store+v.1.0+By+Kvc.zip'

     0K .......... .......... .......... .......... ..........  0% 54.8K 2m24s
    50K .......... .......... .......... .......... ..........  1%  129K 1m42s
   100K .......... .......... .......... .......... ..........  1%  163K 83s
   150K .......... .......... .......... .......... ..........  2%  157K 75s
   200K .......... .......... .......... .......... ..........  3%  194K 67s
   250K .......... .......... .......... .......... ..........  3%  197K 62s
   300K .......... .......... .......... .......... ..........  4%  228K 58s
   350K .......... .......... .......... .......... ..........  5%  222K 54s
   400K .......... .......... .......... .......... ..........  5%  200K 52s
   450K .......... .......... .......... .......... ..........  6%  285K 49s
   500K .......... .......... .......... .......... ..........  6%  168K 48s
   550K .......... .......... .......... .......... ..........  7%  345K 46s
   600K .......... .......... .......... .......... ..........  8%  181K 45s
   650K .......... .......... .......... .......... ..........  8%  202K 44s
   700K .......... .......... .......... .......... ..........  9%  289K 43s
   750K .......... .......... .......... .......... .......... 10%  235K 42s
   800K .......... .......... .......... .......... .......... 10%  302K 40s
   850K .......... .......... .......... .......... .......... 11%  290K 39s
   900K .......... .......... .......... .......... .......... 11%  280K 38s
   950K .......... .......... .......... .......... .......... 12%  294K 37s
  1000K .......... .......... .......... .......... .......... 13%  254K 36s
  1050K .......... .......... .......... .......... .......... 13%  252K 36s
  1100K .......... .......... .......... .......... .......... 14%  301K 35s
  1150K .......... .......... .......... .......... .......... 15%  156K 35s
  1200K .......... .......... .......... .......... .......... 15%  183K 35s
  1250K .......... .......... .......... .......... .......... 16%  197K 35s
  1300K .......... .......... .......... .......... .......... 16%  169K 34s
  1350K .......... .......... .......... .......... .......... 17%  127K 35s
  1400K .......... .......... .......... .......... .......... 18%  166K 35s


This was saved in the text file... As... it seems like it keeps on adding new line after each change. All i need is to read the file from the bottom. And, read it every-time a change is detected.

Really helpful - Thanks for your time and efforts. :)
Any additional advice you wanna give me... for detecting the changes precisely. Or any exe / batch function (I call them plugins.. but..) that can help me :)

Once again... thanks aGerman :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#6 Post by aGerman » 13 Sep 2017 09:51

Kvc wrote:it seems like it keeps on adding new line after each change.

Yes it seems like. But I don't think so because it would be surprising if you would see all these lines in the cmd window whithout the redirection. Exactly that was the reason why I asked you to ZIP the file and upload it. Your pasted text is useless in this case because the real end-of-line characters are changed by the forum software.

Steffen

Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Re: Saving a changing output to a text file... and update the file each time output changes!

#7 Post by Kvc » 14 Sep 2017 10:32

aGerman wrote:Your pasted text is useless in this case because the real end-of-line characters are changed by the forum software.


Opps! I didn't considered that... Here's the file! :roll: :roll: :roll: :lol: :lol: :lol:

Edit 1:
I don't know what extention it wants... but can't upload bat, txt, or any other format... trying zip!
Output.zip
(470 Bytes) Downloaded 329 times


Edit 2:
Yeah! finally learned that.. :D

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#8 Post by pieh-ejdsch » 14 Sep 2017 12:05

Hello,
only times interesting What do you have to do with the file?
Do you want to display the output itself in another input prompt or calculate something else (during the download)?

Phil

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#9 Post by aGerman » 14 Sep 2017 14:21

Surprisingly the end of lines are indeed CR and LF.

To write and read in parallel you have to run wget asynchronously.
Give this a go:

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "outfile=Output.txt"

REM create backspace and carriage return characters
for /f "tokens=1,3" %%a in ('prompt $H$S^&for /f %%b in ^('copy /z "%~f0" nul'^) do %%b 2^>nul') do (set "bs=%%a"&set "cr=%%b")

REM run wget asynchronously
start /b cmd /c "2>"!outfile!" wget "YourURL""

set "line=" &set "before="
REM polling loop
:loop
REM wait a few milliseconds to reduce CPU load
>nul pathping 127.0.0.1 -n -q 1 -p 50
REM read all lines that contain a digit followed by a percent sign, the last line keeps being saved in variable line
for /f "delims=" %%i in ('2^>nul findstr /rc:"[0-9]%% " "!outfile!"') do set "line=%%i"
REM if a new line was read
if "!line!" neq "!before!" (
  REM jump the cursor back to the begin of the line and overwrite the former values
  <nul set /p "=!bs!!cr!!line:~0,7! !line:~62,4! !line:~67,5! !line:~73!    "
  REM save the line for the next comparison
  set "before=!line!"
)
REM jump back to :loop as long as the download wasn't at 100%
if "!line:~62,4!" neq "100%%" goto loop
REM write line end
echo(

pause

Note that you'll end up in an infinite loop if "100%" was never read.

Steffen

Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Re: Saving a changing output to a text file... and update the file each time output changes!

#10 Post by Kvc » 15 Sep 2017 07:16

pieh-ejdsch wrote:Do you want to display the output itself in another input prompt or calculate something else (during the download)?


Yup, I just wanted to create a GUI kind of loading bar... as I just made a Loading function after reading aGerman's reply. And, combined their code with my function. And, adding one line of code made all that complex code into a beautiful GUI output on the CMD.

Image

To write and read in parallel you have to run wget asynchronously.

Yup, we need to initiate that multi-threading thing in batch... :roll: :roll: :roll: :lol: :lol: :lol:

Thank you very much for explaining and help. :)

github link: https://github.com/TheBATeam/Loading-Fu ... 0---By-Kvc
it is not allowing me to upload files bigger than 100kb - so, As I'm not allowed to put my website link here... Thus, i'm learning to understand and use github too. :) :roll: :roll: :roll: :roll: :roll:

here I'm attaching the project file.. if you want to have a look at the thing. And, If you want to suggest anything else - please do that. :)
And, One more thing... can you tell me - how to remove that annoying text line on the first line of console... :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Saving a changing output to a text file... and update the file each time output changes!

#11 Post by aGerman » 15 Sep 2017 07:54

Kvc wrote:And, One more thing... can you tell me - how to remove that annoying text line on the first line of console... :)
I left comments for nearly every line I wrote so it should have been clear what happens here ...

Code: Select all

  REM jump the cursor back to the begin of the line and overwrite the former values
  <nul set /p "=!bs!!cr!!line:~0,7! !line:~62,4! !line:~67,5! !line:~73!    "
... and also as soon as you remove this line you don't need the backspace and carriage return characters anymore.

Steffen

Kvc
Posts: 26
Joined: 13 Jun 2017 06:44
Location: India
Contact:

Re: Saving a changing output to a text file... and update the file each time output changes!

#12 Post by Kvc » 15 Sep 2017 09:45

aGerman wrote:I left comments for nearly every line I wrote so it should have been clear what happens here ...


Oops! My Bad... :) Thanks for the help! And, I really appreciate your help in order to explain much deeper batch concepts via your code. :) :wink: :wink: :wink:

Post Reply