[Batch] sFix - System file repair tool that works even when SFC fails.

Discussion in 'Scripting' started by anemeros, Jan 26, 2011.

  1. anemeros

    anemeros MDL Developer

    Aug 12, 2009
    81
    280
    0
    #1 anemeros, Jan 26, 2011
    Last edited by a moderator: Apr 20, 2017
    UPDATED 2011-03-19

    Here is my batch file rendition of a system file repair tool. I haven't really tested it much, so any feedback you can offer would be great.

    sFix [filename] [architecture]

    [filename]
    the name of the file to repair (e.g. slmgr.vbs).​
    [architecture]
    the target architecture (e.g. x64).​

    Note that most 64bit windows operating systems also contain 32bit files,
    so the [architecture] you specify should refer to the architecture build
    of the target file itself, not necessarily the primary architecture of the
    operating system!

    On a 64bit Windows operating system when running the 64bit CMD interpreter:
    64bit system files can be accessed from the "System32" folder.
    32bit system files can be accessed from the "SysWOW64" folder.​

    On a 64bit Windows operating system when running the 32bit CMD interpreter:
    64bit system files can be accessed from the "Sysnative" virtualization folder.
    32bit system files can be accessed from the "System32" redirected folder.​

    On a 32bit Windows operating system:
    64bit system files cannot be used and are not present.
    32bit system files can be accessed from the "System32" folder.​


    USAGE EXAMPLES:
    • sFix winver.exe x86
    • sFix winver.exe x64

    Code:
    :: sfix 0.4 by anemeros
    :: restores system files from their winsxs originals
    :: params = [filename] [architecture]
    
    :h
    @echo off
    setlocal enableextensions
    if "%~1" equ "" (goto :help)
    echo.%*|find "?">nul
    if %ERRORLEVEL% equ 0 (goto :help)
    set tgtpath=%SystemRoot%\System32
    echo.%1|find /i "\syswow64">nul
    if %ERRORLEVEL% equ 0 (set tgtpath=%SystemRoot%\SysWOW64)
    set arch1=
    echo.%2|find "64">nul
    if %ERRORLEVEL% equ 0 (set arch1=x64)
    echo.%2|find "32">nul
    if %ERRORLEVEL% equ 0 (set arch1=x86)
    echo.%2|find "86">nul
    if %ERRORLEVEL% equ 0 (set arch1=x86)
    if "%arch1%" equ "" (echo.Invalid target architecture specification.&exit /b 1)
    if exist "%SystemRoot%\SysWOW64\cmd.exe" (set arch0=x64) else (set arch0=x86)
    if "%arch0%" equ "x86" if "%arch1%" equ "x64" (echo.There are no 64bit system files on a 32bit windows operating system.&exit /b 1)
    if "%arch0%" equ "x64" if "%arch1%" equ "x86" (set tgtpath=%SystemRoot%\SysWOW64)
    set tgt=%tgtpath%\%~1
    call :maketemps
    
    :m
    for /f "usebackq" %%n in (`dir /s /b "%SystemRoot%\winsxs\%~1"^|find /i "%arch1:~1%_microsoft"`) do set src=%%n
    if "%src%" equ "" (echo. - No file was copied; the backup was not found.&exit /b 1)
    for /f "tokens=4-5 delims=\_" %%a in ("%t%") do echo.source:  %%b (%%a)
    echo.target:  %tgt%
    echo.
    echo.status:
    echo.
    icacls "%tgt%" /save %ttacl%>nul
    takeown /f "%tgt%">nul
    icacls "%tgt%" /grant *s-1-1-0:f>nul
    move /y "%tgt%" %trash%>nul
    copy /y "%src%" "%tgt%">nul
    if %ERRORLEVEL% neq 0 (
    icacls "%tgtpath%" /restore %ttacl%>nul
    echo. - The target file was not successfully repaired with its backup counterpart.
    move /y %trash% "%tgt%">nul||(echo. - A self-heal transfer error has occurred.&echo. - Please move %trash% to "%tgt%" manually.)
    exit /b 1
    )
    icacls "%tgtpath%" /restore %ttacl%>nul
    echo. - Target file was replaced successfully.
    if %ERRORLEVEL% neq 0 (echo. - Original file ACLs were not successfully restored.&exit /b 2) else (echo. - Original file ACLs were applied successfully.)
    exit /b 0
    
    :t
    set tmpfile="%TEMP%\f%RANDOM%%RANDOM%.%~1"
    if exist %tmpfile% (goto :t) else (goto :eof)
    
    :maketemps
    call :t acl
    set ttacl=%tmpfile%
    call :t tmp
    set trash=%tmpfile%
    goto :eof
    
    :help
    echo.sFix 0.4 [by anemeros]
    echo.  restores system files from their windows original backups
    echo.
    echo.sFix [filename] [architecture]
    echo.
    echo.  [filename]        the name of the file to repair (e.g. slmgr.vbs).
    echo.  [architecture]    the target architecture (e.g. x64).
    echo.
    echo.Note that most 64bit windows operating systems also contain 32bit files,
    echo.so the [architecture] you specify should refer to the architecture build
    echo.of the target file itself, not necessarily the primary architecture of the
    echo.operating system!
    echo.
    echo.On a 64bit Windows operating system when running the 64bit CMD interpreter:
    echo.  64bit system files can be accessed from the "System32" folder.
    echo.  32bit system files can be accessed from the "SysWOW64" folder.
    echo.
    echo.On a 64bit Windows operating system when running the 32bit CMD interpreter:
    echo.  64bit system files can be accessed from the "Sysnative" virtualization folder.
    echo.  32bit system files can be accessed from the "System32" redirected folder.
    echo.
    echo.On a 32bit Windows operating system:
    echo.  64bit system files cannot be used and are not present.
    echo.  32bit system files can be accessed from the "System32" folder.
    goto :eof
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    Thank Anemeros really is a great script, 100% working ...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. MadBox

    MadBox MDL Novice

    Jan 31, 2011
    1
    0
    0
    and how to use it?
     
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    hi anemeros,

    here sfix is failing to restore files to the system32 folder on x64 systems, claims file not found ...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    anemeros

    I analyzed your code, and discovered it was just a simple mistake

    winsxs folders, files x64, is the beginning of the folder with "amd64"and not "x64", as shown in your code

    to fix this, simply replace all the words x64 for amd64, and work properly

    thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. anemeros

    anemeros MDL Developer

    Aug 12, 2009
    81
    280
    0
    Thanks for noticing that Josh. I think I've fixed it in 0.4, which I've posted above. Let me know if you have any trouble with it.
     
  7. gaza_dan

    gaza_dan MDL Member

    Jan 9, 2011
    157
    60
    10
    nice tool this will be useful for thise who have problems installing loaders when certain system files are missing
     
  8. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    thanks for the great update
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. anemeros

    anemeros MDL Developer

    Aug 12, 2009
    81
    280
    0
    Do you use sFix in MBR Regenerator, Josh?
     
  10. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    Yes, I asked a while ago and you gave me that permission ...

    I was thinking of it fits in the program a few months, only now gathered the ideas and completes the update ...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. piggynuts

    piggynuts MDL Novice

    Apr 15, 2013
    1
    0
    0
    how to use this batch file????

    can someone please explain a little more in detail how to use this batch

    i copied the code to a text file and made it to a .bat
    i then opened a cmd in admin
    i then did a c:\batch.bat sfix random.dll x32
    getting errors

    i have no idea if im doing this correctly.

    also, do i have to know the file that sfc cannot fix? or does it find it automatically? please someone give a step by step of how to use this if possible, because if it works it will be an amazing tool, i spend so much time doing repair installs at work for customers.
     
  12. Tito

    Tito Super Mod / Adviser
    Staff Member

    Nov 30, 2009
    18,681
    18,589
    340