read sfc output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

read sfc output

#1 Post by einstein1969 » 30 Mar 2021 15:24

Hi,

I need to read the output of the command "sfc /scannow" with dos batch/powershell.

I have searched the web and there are some solutions but nothing very clear to me.

i would use dos batch commands if possible and powershell as well.

It is possible?

Einstein1969

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: read sfc output

#2 Post by Squashman » 30 Mar 2021 15:31

The first thing I would check if it is writing everything to the STDOUT, STDERR or CON. If you redirect standard error to a file, then you know it is writing to STDERR. If your redirect STDOUT to a file and it doesn't go to file then it is writing directly to the console and you will not be able to capture the output.

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

Re: read sfc output

#3 Post by aGerman » 30 Mar 2021 16:02

I'm not sure if that is something you can make use of. However, sfc already writes to the cbs.log file. You just have to filter the entries that contain the [SR] tag.
https://docs.microsoft.com/en-us/troubl ... le-entries

Steffen

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: read sfc output

#4 Post by einstein1969 » 31 Mar 2021 03:06

@squashman

This is the output on console when execute "sfc /scannow" on cmd windows:

Code: Select all

Avvio in corso dell'analisi del sistema. Attendere. L'operazione richiederà alcuni minuti.

Avvio in corso della fase di verifica dell'analisi del sistema.
2% della verifica completato.

- Is there a way to capture the CMD screen buffer (with powershell or other)? That is to automate the copy and paste we do with the mouse?

When i redirect on file "sfc /scannow >out.log 2>err.log" the output on screen don't show.

The out.txt is not written immediatly (I think is buffered).

Code: Select all

C:\Users\fposc\AppData\Local\Temp>dir out.log err.log
 Il volume nell'unità C è Windows
 Numero di serie del volume: 0F35-6BA0

 Directory di C:\Users\fposc\AppData\Local\Temp

31/03/2021  10:55                 0 out.log

 Directory di C:\Users\fposc\AppData\Local\Temp

31/03/2021  10:55                 0 err.log
               2 File              0 byte
               0 Directory  15.222.960.128 byte disponibili
so i can't use in this manner.

@steffen

This is the code that already i done for simulating progress but it is not enough for me, I ask help at you..

I have see that on my machine the output of sfc is written the first time on disk after 33% of work. But i think this is not true for other pc.

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: By Francesco Poscetti aka einstein1969 - for www.tenforums.com
::
:: ver. 0.2 - 27/03/2021 added percentage and remaining time on SFC part
:: ver. 0.1 - 21/03/2021 
::
:: Reference:
:: https://docs.microsoft.com/en-us/troubleshoot/windows-client/deployment/analyze-sfc-program-log-file-entries
:: https://support.microsoft.com/it-it/topic/description-of-the-windows-file-protection-feature-db28f515-6512-63d1-6178-982ed2022ffb
:: https://docs.microsoft.com/it-it/windows/win32/wfp/protected-file-list
:: https://docs.microsoft.com/it-it/windows/win32/wfp/detecting-file-replacement (C++ code)
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableDelayedExpansion & if "%1" == "Progress" goto :Progress

mode con cols=62 lines=5
cd %TMP%

( 
  del stop.tmp 2>nul
  sfc /scannow >out.txt
  type nul > stop.tmp
) | %0 Progress SFC

pause
cls

( 
  del stop.tmp 2>nul
  DISM /online /cleanup-image /scanhealth >out.txt
  type nul > stop.tmp
) | %0 Progress DISM

pause

goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Progress
  Title %2: Please wait...
  echo %2 is running...

  for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
goto :Progress_%2
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Progress_DISM
for /L %%N in (0) do (
  for /f "tokens=*" %%S in ('more out.txt') do (
    set u=%%S
    if not "x!u:[=!"=="x!u!" set v=%%S
  ) 
  if defined v  (
    title %2: [!v:~29,5!]
    set /P "=.!CR!!v!!CR!" < nul
  )
  if exist stop.tmp echo(&exit
  ping -n 5 127.0.0.1 >nul 
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Progress_SFC

set "CBS.log=%windir%\Logs\CBS\CBS.log"

for /f %%L in ('FIND /V /C "" ^< %CBS.log%') do set /A LN=%%L-1

set "count_files=for /f "tokens=*" %%S in ('More %CBS.log% +!LN! ^^| findstr /c:"[SR]" ^^| findstr /C:"Verifying 100" ^^| FIND /V /C ""') do set /A v=%%S0*10"

Set "v33=#"
Set t0=%time%
for /L %%N in (0) do (
  Set /A "c=(c+1)%%10"
  if !c! equ 1 (
    %count_files%
    if not defined v33 (
      for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!time: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, a=a/100"
      Set /A "P#=v*1000/NF" & (if !P#! gtr 1000 Set "P#=1000") & Set "P=(!P#:~0,-1!.!P#:~-1!%%)"
      Set /A "t=a*1000/P#, r=t-a, s=r%%60, r/=60, m=r%%60, r/=60, h=r%%60, HMS=s+m*100+h*10000+1000000" & Set "r=Time left -> !HMS:~1,2!:!HMS:~3,2!:!HMS:~5!"
      title %2: !P! about !NF! files to process...
    )
    set /p "=.!CR!Processed !v! files... !P! !r! !CR!" <NUL
  )
  if defined v33 (
    For %%# in ("out.txt") Do Set size=%%~z#
    if !size! neq 0 (
      if !c! neq 1 %count_files%
      set /A "NF=(v-200)*3" & set "v33="
      copy out.txt out2.txt >nul
    )
  )
  if exist stop.tmp echo(&exit
  ping -n 2 127.0.0.1 >nul
)
goto:eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: read sfc output

#5 Post by Compo » 31 Mar 2021 08:17

There's some interesting observations made in the following StackOverflow post, which may potentially assist you.

Post Reply