Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Batch to Log & Console

- - - - -

  • Please log in to reply
20 replies to this topic

#1
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Hello

I'm trying to add a log export ability to my Run on first boot batch file after an XP silent installation. I want to know if everything went fine (or not) so I can tweak further and for reference as well.

I really don't understand batch much, not at least at this level. I found this example that renders out everything shown in the console window, which is fine, but in return now the console is blank.

@ECHO ON
call :Logit>>C:\mylog.log 2>&1
exit /b 0
:Logit

::Your bat code here

Then the following code examples IIRC were the opposite, show everything on the console and log only the commands you attach /log flag to.
That's what I tried, but I got no log. Do I need to add something else somewhere, what am I missing?

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file

Thank you!

Edited by Dogway, 07 February 2013 - 03:29 PM.



How to remove advertisement from MSFN

#2
5eraph

5eraph

    Update Packrat

  • MSFN Sponsor
  • 1,099 posts
  • OS:XP Pro x64
  • Country: Country Flag

Donator

Try changing the following line:

if /I ?%1? NEQ ?/log? goto :LogIt
...to this...

if /I not "%~1"=="/log" goto :LogIt


#3
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
I changed the line but nothing happens. I have the log code first, then the variables, then the below command.
%ENCODER% --level 4.1 --preset %PRESET% --tune %TUNE% --pass 1 --bitrate %BITRATE% --stats "%STATS%" --me %ME% --deblock -2:-1 --keyint %KEY% --min-keyint %MKEY% --frames %FRAMES% --demuxer %DEMUX% --vbv-bufsize 24000 --no-fast-pskip --sar %SAR% --aq-strength 1.2 --vbv-maxrate 24000 --fps %FPS% --output NUL "%INPUT%" /log


#4
Yz÷wl

Yz÷wl

    Wise Owl

  • Super Moderator
  • 4,506 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

I would suggest something along these lines:

MyScript.cmd

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "_OUT=CALL :SUB"
FOR /F "DELIMS=/-" %%# IN ("%*") DO (
	IF /I %%# EQU L SET "_OUT=(%_OUT%)>MYLOG.LOG 2>&1")
%_OUT%
GOTO :EOF
:SUB
	your command(s) here
This means that your script will accept L, l, /L, /l, -L or -l as parameters for logging the output to file. anything else should log the output to the console window.

for this to work your script should have no spaces in its file name or at least be quoted

#5
jaclaz

jaclaz

    The Finder

  • Developers
  • 13,406 posts
  • OS:none specified
  • Country: Country Flag
You might be interested in a TEE command :unsure: :
http://www.robvander...m/unixports.php

jaclaz

#6
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Thanks a lot Yzöwl, but again no log is output, here is my script, did I miss something? (edit: my bat is called hey.bat so no spaces)
@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "_OUT=CALL :SUB"
FOR /F "DELIMS=/-" %%# IN ("%*") DO (
        IF /I %%# EQU L SET "_OUT=(%_OUT%)>MYLOG.LOG 2>&1")
%_OUT%
GOTO :EOF
:SUB

::Your bat code here
D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs /L

jaclaz, that's a bit over my head, at least for the time being, I don't discard to have a deeper look later on.

#7
Yz÷wl

Yz÷wl

    Wise Owl

  • Super Moderator
  • 4,506 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

You are supposed to run my batch script, MyScript.cmd with or without the parameter, not your command within the script!

examples

MyScript
or
MyScript -l


#8
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
I see I was quite confused!

Now I used
hey.cmd -L

And yes I get the log, but console is blank

#9
Yz÷wl

Yz÷wl

    Wise Owl

  • Super Moderator
  • 4,506 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

that is because you asked for the information to be output to a file.

If you want it to be logged to two places simultaneously then you need to tee it, as jaclaz has already indicated.
You can either use a 3rd party utility for that purpose or possibly this vbscript.

tee.vbs

set con = createobject("scripting.filesystemobject").opentextfile("con:", 2)
do until wsh.stdin.atendofstream
	s = wsh.stdin.readline
	wsh.echo s : con.writeline s 
loop
to use it you'd probably just pipe your script into it.

hey.cmd

@D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs
Then run it like this:
hey.cmd | cscript tee.vbs >mylog.log


#10
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Woow, indeed I'm not asking something easy. Thanks a bunch for the help, the log is blank, well I get this:
Microsoft (R) Windows Script Host versi¢n 5.7
Copyright (C) Microsoft Corporation 1996-2006. Reservados todos los derechos.

but I see the point.

Yes, the title is misleading but in my OP I made clear I wanted to output to both, console and log, at the same time.


EDIT:
Now I'm using jaclaz's suggestion. In the same vein:
Downloaded Tee.bat and make the call as:
hey.cmd  ¦  TEE.BAT  [ -a ]  C:\OEM\mylog.log
but I don't get any log. Should be the bat encoded in some format? different from ANSI? is the ¦ character fine.

Edited by Dogway, 07 February 2013 - 03:54 PM.


#11
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Ok, for the second alternative (using TEE.BAT) I managed to output something.
I had to add > before the log filename, and also remove the @ before the command in the bat file.

But, all I get in the log is the question, my input command:
D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs

and not my answer (or Q&A together):
C:\OEM>D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs 
avs [info]: 720x480p 0:0 @ 30000/1001 fps (cfr)
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
x264 [info]: profile Main, level 4.1
[1.0%] 1/101 frames, 1.10 fps, 2075.60 kb/s, eta 0:01:30  
[14.9%] 15/101 frames, 12.98 fps, 1109.24 kb/s, eta 0:00:06  
[34.7%] 35/101 frames, 24.89 fps, 1600.93 kb/s, eta 0:00:02  
[74.3%] 75/101 frames, 45.29 fps, 1476.59 kb/s, eta 0:00:00  
                                                                               
x264 [info]: frame I:1     Avg QP:21.54  size:  8657
x264 [info]: frame P:29    Avg QP:18.16  size:  9658
x264 [info]: frame B:71    Avg QP:18.83  size:  4739
x264 [info]: consecutive B-frames:  2.0%  2.0% 32.7% 63.4%
x264 [info]: mb I  I16..4: 87.7%  0.0% 12.3%
x264 [info]: mb P  I16..4: 59.9%  0.0%  0.0%  P16..4: 32.6%  0.0%  0.0%  0.0%  0.0%    skip: 7.6%
x264 [info]: mb B  I16..4: 18.1%  0.0%  0.0%  B16..8: 31.6%  0.0%  0.0%  direct:29.6%  skip:20.7%  L0:42.6% L1:43.7% BI:13.7%
x264 [info]: final ratefactor: 18.81
x264 [info]: direct mvs  spatial:97.2% temporal:2.8%
x264 [info]: coded y,uvDC,uvAC intra: 45.7% 62.9% 8.7% inter: 19.3% 46.7% 0.5%
x264 [info]: i16 v,h,dc,p: 37% 28% 28%  7%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 11% 23% 24%  7%  6%  4%  8%  5% 12%
x264 [info]: i8c dc,h,v,p: 53% 23% 23%  2%
x264 [info]: Weighted P-Frames: Y:0.0% UV:0.0%
x264 [info]: kb/s:1484.21

encoded 101 frames, 56.20 fps, 1484.21 kb/s


#12
allen2

allen2

    Not really Newbie

  • Members
  • PipPipPipPipPipPipPip
  • 1,812 posts
x264 encoder use the error channel for its normal output so if you need to log its output, you'll need to add "1>NUL 2>%temp%\x264_encoding.log" for example to the line launching it.
For the second point as you need the output in the console and in the log file, you could try to launch x264 with log (as explained before) as background process and then print the log with "tail -f" (from the unix tools).
So something like this will work:
start /B /separate "encoding" x264.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs 1>NUL 2>%temp%\log_x264.log
tail.exe -f %temp%\log_x264.log
As tail -f will wait forever, and as the --pid option doesn't work in this dos port (and some newer mingw32 port), you'll have to interrupt it with a control+C after its ending or kill tail process. This lastest option might be automated if you don't run many x264 process at the same time as batch doesn't provide any simple way of getting the pid of a launched process.


Edit1: as a side note, doing this with a higher programming language (C++, VB, vbs, autoit ....) will be a lot easier.

Edit2:
In batch using third party tools - unix tools sleep, tail, gawk, wc and sysinternals handle -, i was able to make it work:
@echo off
start /B /separate "encoding" x264.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL E:\encodage\dimitri.avs 1>NUL 2>%temp%\log_x264.log
start /B /separate "logging" d:\utils\command\tail.exe -f %temp%\log_x264.log
setlocal
:wait
set ended=0
for /f "usebackq" %%i in (`handle %temp%\log_x264.log ^|findstr /i "pid:" ^|wc -l`) do (if "%%i"=="1" set ended=1)
if "%ended%"=="1" goto killpid
sleep 1
goto wait
:killpid
handle %temp%\log_x264.log |findstr /i "pid:" |gawk -F"pid:" "{print $2}" |gawk "{print $1}" >%temp%\tail_pid.txt
for /F %%i in (%temp%\tail_pid.txt) do (taskkill /pid %%i /t  /f >NUL)
:end
endlocal
pause


Edited by allen2, 09 February 2013 - 04:49 AM.


#13
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Wonderful, this almost feel like hacking!
I think using x264 wasn't the best example since it's only for my tests. I will use the logging for the "Install at first Boot" batch of an OS install, so I don't think I will have the described problems.

Most likely I will get along with the TEE.BAT route.
I also used your first example with tail.exe. It exported the answer (error log), I guess this is all I want to know (discarding the call line). Also I can anyways run a kill process command for tail.exe.
So I should always append the next for any of my commands right?
start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
...

I couldn't run your second example, I got a BSOD and it scared the hell out of me. I think with all these example it's more than enough.

Edited by Dogway, 09 February 2013 - 11:58 AM.


#14
jaclaz

jaclaz

    The Finder

  • Developers
  • 13,406 posts
  • OS:none specified
  • Country: Country Flag

So I should always append the next for any of my commands right?


No.
You need to understand redirection (and the concepts of "standard output" and "standard error" first:
http://www.robvander...redirection.php

Every program might "decide" to write *something* to standard output and *something else* to standard error, so you should append BOTH standard output and standard error to the log:
command >> file 2>&1

jaclaz

#15
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
I see.

This?
start /B /separate (mycommands+flags) >> %temp%\log_x264.log 2>&1
start /B /separate (mycommands+flags) >> %temp%\log_x264.log 2>&1
...

I guess it was ignoring call line because I was writing 1>NUL

#16
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
My laptop crashed a month ago.
I'm back again and now I tried with a more on earth situation, using simple commands.

I created a test rar: link

basically this is the content:

TEE.bat in the folder

Run.bat
prueba.cmd  ^|  TEE.BAT  [ -a ]  >mylog.txt

prueba.cmd
xcopy /Y "Banco\Copiar.txt" "Banco/Folder"
ping localhost -n 5 >nul

ECHO.
ECHO Removing installations logs and other interesting crap
ECHO Please wait...
DEL "Banco\Borrar.txt"
rd /s /q  "Banco/Folder"

I hit run and it processes everything fine and exports to log, but I don't see anything in console. Do I need tail.exe or something? If I use the TEE.VBS variant I get the output on screen and log at the same time, but only the command answers, not the questions as tee.bat does)


EDIT: OK, I solved it! Finally I used the TEE.VBS variant. In order to make it output command calls as well I had to remove the @ signs, and now I have full log on console screen and log at the same time. If someone still wants to help with the Tee.bat method I'd be welcome, for the sake of learning.

Edited by Dogway, 12 March 2013 - 12:38 AM.


#17
Yz÷wl

Yz÷wl

    Wise Owl

  • Super Moderator
  • 4,506 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

prueba.cmd

xcopy /Y "Banco\Copiar.txt" "Banco/Folder"
ping localhost -n 5 >nul

ECHO.
ECHO Removing installations logs and other interesting crap
ECHO Please wait...
DEL "Banco\Borrar.txt"
rd /s /q  "Banco/Folder"

Thanks for using the method I provided which uses built-in tools and requires no third party utilities. I will add that I see little point in using a batch file which calls a batch file which calls a utility over one which simply calls a vbscript..

One question, have you deliberately removed a section of your script?
If not, you're performing tasks for no reason, there is no point in copying a file to a directory then removing that directory


#18
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
Yes, Yz÷wl, there's no point, it's just a staged test (dummy) example to test things out. On the real script I will be installing programs and patches.
Thanks for all the help!

#19
PROBLEMCHYLD

PROBLEMCHYLD

    The Resurrector for old Windows OS

  • Members
  • PipPipPipPipPipPipPipPip
  • 2,528 posts
  • OS:98SE
  • Country: Country Flag
Can you log registry changes this way?

#20
Dogway

Dogway

    Advanced Member

  • Members
  • PipPipPip
  • 357 posts
  • OS:XP Pro x86
  • Country: Country Flag
I must say that it still isn't perfect at all. It gives me almost the full thing, but fails and errors aren't passed to the log, probably because as commented above they are passed through the error channel, making my original purpose meaningless, since all I wanted with this is to check errors after all. Anyways, I'm not concerned anymore so if anyone want to help, then it's welcomed.

#21
Yz÷wl

Yz÷wl

    Wise Owl

  • Super Moderator
  • 4,506 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

If you are installing software/applications you cannot expect that each developers installer will have built in an error logging procedure which can be output through a command line interface.

For instance, Windows Installer uses the /L switch for logging to a file.

In some cases, you may be better advised outputting all to a log file then displaying that output to the console.

Also if it is only the error stream you are interested in have you tried changing the tee.vbs to read:
set con = createobject("scripting.filesystemobject").opentextfile("con:", 2)
do until wsh.stdin.atendofstream
        s = wsh.stdin.readline
        wsh.stderr.writeline s : con.writeline s
loop





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users



How to remove advertisement from MSFN