@echo off

set installs=0
set count=0

title Reparacion sigma error by KHCOMPANY for C++ Runtime (Version 1.0.5)
ver|"%windir%\system32\findstr.exe" /c:" 5."
if %errorlevel% equ 0 (
echo.
echo Las versiones de Windows anteriores a Windows Vista no son compatibles.
echo No se han hecho cambios.
echo.
echo.
echo Presione cualquier tecla para salir del instalador.
pause >nul
goto :eof
)


if defined PROCESSOR_ARCHITEW6432 (set arch=x64) else (set arch=x86)
"%windir%\system32\reg.exe" query "hklm\software\microsoft\Windows NT\currentversion" /v buildlabex >"%temp%\os.txt"

for /f "tokens=3* delims= " %%G in ('reg query "hklm\software\microsoft\Windows NT\currentversion" /v productname') do (set winv=%%G %%H)
echo %winv%|find /i "Windows 10" >nul
if errorlevel 0 (set w10=1&for /f "tokens=3" %%G in ('reg query "hklm\software\microsoft\Windows NT\currentversion" /v UBR') do (set /a UBR=%%G))

if defined w10 (for /f "skip=2 tokens=3,4,6,7 delims=. " %%G in ('type "%temp%\os.txt"') do (set "win=%winv% %arch% Build %%G.%UBR% {%%I %%J}")
) else (
for /f "skip=2 tokens=3,4,6,7 delims=. " %%G in ('type "%temp%\os.txt"') do (set "win=%winv% %arch% Build %%G.%%H {%%I %%J}")
)
del "%temp%\os.txt"

if %arch% equ x64 (for /f %%G in ('type "redists_x64.txt"') do (set /a installs+=1))
for /f %%G in ('type "redists_x86.txt"') do (set /a installs+=1)

if defined auto goto :proceed

if exist "%windir%\winsxs\pending.xml" goto :pending
"%windir%\system32\reg.exe" query "hklm\SYSTEM\CurrentControlSet\Control\Session Manager" /f "\??\C:" >nul
if %errorlevel% equ 0 goto :pending

:top
call :title
echo *****************************************************************
echo Reparacion de error by KHCOMPANY 
echo NO OLVIDEN SUSCRIBIRSE A EL CANAL Y DAR LIKE  
echo *****************************************************************
echo Before installing the latest Visual C++ Runtimes existing
echo versions should be removed.
echo.
echo It is recommended that you only run this on a fresh Windows
echo session, such as after a reboot, and that you close all other
echo programs before proceeding.
echo.
echo Would you like to remove current Visual C++ Runtimes and
echo install those packaged with this installer?
echo.
echo Selecting 'N' for 'No' will exit this installer.
echo.
echo.
echo.
choice /c YRN /n /m "Press Y for Yes, R for Readme, or N to cancel and exit> "

if errorlevel 3 goto :eof
if errorlevel 2 goto :readme
if errorlevel 1 goto :proceed

:proceed
if exist "%temp%\list.txt" (del "%temp%\list.txt")
if exist "%temp%\list2.txt" (del "%temp%\list2.txt")

if %arch% neq x64 goto :x64skip
call :title
echo Uninstalling existing Visual C++ x86 Runtime Redistributables
echo (please wait as this process may take a few moments)

reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 Preview Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 CTP Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 Preview Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 CTP Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 Redistributable" /s >>"%temp%\list2.txt"

if exist "%temp%\list2.txt" (for /f "delims=\ tokens=8" %%a in ('type "%temp%\list2.txt"') do (if exist "C:\ProgramData\Package Cache\%%a\vcredist_x86.exe" ("C:\ProgramData\Package Cache\%%a\vcredist_x86.exe" /uninstall /norestart /quiet) else (if exist "C:\ProgramData\Package Cache\%%a\vcredist_x64.exe" ("C:\ProgramData\Package Cache\%%a\vcredist_x64.exe" /uninstall /norestart /quiet))))

if exist "%temp%\list2.txt" (for /f "delims=\ tokens=8" %%a in ('type "%temp%\list2.txt"') do (if exist "C:\ProgramData\Package Cache\%%a\vc_redist.x86.exe" ("C:\ProgramData\Package Cache\%%a\vc_redist.x86.exe" /uninstall /norestart /quiet) else (if exist "C:\ProgramData\Package Cache\%%a\vc_redist.x64.exe" ("C:\ProgramData\Package Cache\%%a\vc_redist.x64.exe" /uninstall /norestart /quiet))))

reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2005 Redistributable" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2008 Redistributable" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2010  x86 Redistributable" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x86 Minimum Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x86 Additional Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x86 Minimum Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x86 Additional Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x86 Minimum Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x86 Additional Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x86 Minimum Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x86 Additional Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x86 Minimum Runtime" /s >>"%temp%\list.txt"
reg query hklm\software\wow6432node\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x86 Additional Runtime" /s >>"%temp%\list.txt"

if exist "%temp%\list.txt" (for /f "delims=\ tokens=8" %%a in ('type "%temp%\list.txt"') do ("%windir%\system32\msiexec.exe" /X%%a /q /norestart))

if exist "%temp%\list.txt" (del "%temp%\list.txt")
if exist "%temp%\list2.txt" (del "%temp%\list2.txt")

:x64skip
call :title
echo Uninstalling existing Visual C++ %arch% Runtime Redistributables
echo (please wait as this process may take a few moments)

reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 Preview Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 CTP Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 Preview Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 CTP Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 RC Redistributable" /s >>"%temp%\list2.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 Redistributable" /s >>"%temp%\list2.txt"

if exist "%temp%\list2.txt" (if %arch% equ x86 (for /f "delims=\ tokens=7" %%a in ('type "%temp%\list2.txt"') do (if exist "C:\ProgramData\Package Cache\%%a\vcredist_x86.exe" ("C:\ProgramData\Package Cache\%%a\vcredist_x86.exe" /uninstall /quiet))))

if exist "%temp%\list2.txt" (if %arch% equ x86 (for /f "delims=\ tokens=7" %%a in ('type "%temp%\list2.txt"') do (if exist "C:\ProgramData\Package Cache\%%a\vc_redist.x86.exe" ("C:\ProgramData\Package Cache\%%a\vc_redist.x86.exe" /uninstall /quiet))))

reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2005 Redistributable" /s >>"%temp%\list.txt"
reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2008 Redistributable" /s >>"%temp%\list.txt"


if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "C++ 2010  x64 Redistributable" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "C++ 2010  x86 Redistributable" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x64 Minimum Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x86 Minimum Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x64 Additional Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2012 x86 Additional Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x64 Minimum Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x86 Minimum Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x64 Additional Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2013 x86 Additional Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x64 Minimum Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x86 Minimum Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x64 Additional Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 14 x86 Additional Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x64 Minimum Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x86 Minimum Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x64 Additional Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2015 x86 Additional Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x64 Minimum Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x86 Minimum Runtime" /s >"%temp%\list.txt")

if %arch% equ x64 (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x64 Additional Runtime" /s >>"%temp%\list.txt") else (reg query hklm\software\microsoft\windows\currentversion\uninstall /f "Microsoft Visual C++ 2017 x86 Additional Runtime" /s >"%temp%\list.txt")

if exist "%temp%\list.txt" (for /f "delims=\ tokens=7" %%a in ('type "%temp%\list.txt"') do ("%windir%\system32\msiexec.exe" /X%%a /q))

for /f %%G in ('type "redists_x86.txt"') do (call :install "%%G")
if %arch% equ x64 (for /f %%G in ('type "redists_x64.txt"') do (call :install "%%G"))
if %arch% equ x64 (copy /y old_runtimes\* "%windir%\syswow64">nul) else (copy /y old_runtimes\* "%windir%\system32">nul)
if %arch% equ x64 ("%windir%\system32\regsvr32.exe" "%windir%\syswow64\comctl32.ocx" "%windir%\syswow64\comdlg32.ocx" "%windir%\syswow64\mscomctl.ocx" /s) else ("%windir%\system32\regsvr32.exe" "%windir%\system32\comctl32.ocx" "%windir%\system32\comdlg32.ocx" "%windir%\system32\mscomctl.ocx" /s)

if exist "%temp%\list.txt" (del "%temp%\list.txt")
if exist "%temp%\list2.txt" (del "%temp%\list2.txt")

if defined auto goto :eof

call :title
echo Installer has completed. 

if exist "%windir%\winsxs\pending.xml" (echo.&echo Your system has pending file operations&echo Please reboot as soon as possible!)
echo.
echo.
echo.
echo Press any key to exit...
pause >nul
goto :eof


:install
call :title
set /a count+=1
echo Installing package %count% of %installs%: %1
echo.
echo.
echo Each package may take a moment, please be patient!
"%1" /q
goto :eof


:title
cls
echo -----------------------------------------------------------
echo Reparacion error sigma by KHCOMPANY (Version 1.0.5)
echo -----------------------------------------------------------
echo %win%
echo.
goto :eof


:readme
cls
echo ------------------------------------------------------------------------------
echo Reparacion error sigma by KHCOMPANY (Version 1.0.5) Readme Page 1 of 2
echo ------------------------------------------------------------------------------
echo %win%
echo.
echo Este instalador instala automáticamente los últimos tiempos de ejecución de Visual C ++
echo disponible en el momento en que se hizo esta versión del instalador.
echo.
echo Los tiempos de ejecución incluidos son los últimos tiempos de ejecución completos de Visual C ++ 
echo para las versiones de Visual C ++ 2005, 2008, 2010, 2012, 2013 y 2015/2017. Esto incluye
echo tiempos de ejecución x86 y x64. En sistemas de 32 bits, solo los tiempos de ejecución de 32 bits
echo están instalados
echo. 
echo.
echo.
echo *****************************************************************
echo Reparacion de error by KHCOMPANY 
echo NO OLVIDEN SUSCRIBIRSE A EL CANAL Y DAR LIKE  
echo *****************************************************************
echo.
echo.
echo.
echo.
choice /c NM /N /M "N - Next page, M - Return to Installer"

if errorlevel 2 goto :top
if errorlevel 1 goto :page2

:page2
cls
echo ------------------------------------------------------------------------------
echo Reparacion error sigma by KHCOMPANY (Version 1.0.5) Readme Page 2 of 2
echo ------------------------------------------------------------------------------
echo %win%
echo.
echo Before installation the script needs to remove existing Visual C++
echo Runtimes.  You will be asked whether you want to proceed and
echo uninstall existing Visual C++ Runtimes (recommended) or cancel and
echo exit the installer. To proceed simply press Y (yes), which will
echo automatically uninstall existing Runtimes and install those packaged
echo included with this installer.
echo.
echo.
echo.
echo.
echo.
echo.
choice /c PM /N /M "P - Previous Page, M - Return to Installer"

if errorlevel 3 goto :top
if errorlevel 2 goto :page2
if errorlevel 1 goto :readme


:pending
call :title
echo.
echo Su sistema tiene operaciones de archivo pendientes. El sistema debe reiniciarse
echo para garantizar una instalacion adecuada
echo.
echo ¿Desea reiniciar su computadora ahora? Seleccionando 'N' para 'No'
echo cancelara la instalacion. Si en un nuevo reinicio puede seleccionar 'I'
echo para ignorar e instalar.
echo.
echo Si selecciona sí, guarde todo el trabajo sin terminar, como cualquier no guardado
echo el trabajo se perdera con el reinicio del sistema.
echo.
echo *****************************************************************
echo Reparacion de error by KHCOMPANY 
echo NO OLVIDEN SUSCRIBIRSE A EL CANAL Y DAR LIKE  
echo *****************************************************************
echo.
echo.
echo.
choice /c YNI /n /m "Press Y for Yes, N to cancel and exit, or I to ignore message and continue> "

if errorlevel 3 goto :top
if errorlevel 2 goto :eof
if errorlevel 1 "%windir%\system32\shutdown.exe" /r /f /t 0