@echo off
:: ============================================================================
::  All in One Visual C++ Runtimes Installer  v2.0 (November 2026)
::  https://www.itechtics.com/microsoft-visual-c-redistributable-versions-direct-download-links/
::
::  Installs every Microsoft Visual C++ Redistributable a Windows PC can need:
::  2005, 2008, 2010, 2012, 2013 and the latest v14 (Visual Studio 2015-2026),
::  in the right architectures for this PC (x86, x64, ARM64).
::
::  - Downloads only from Microsoft's own servers (download.microsoft.com,
::    download.visualstudio.microsoft.com, aka.ms) and refuses to run any file
::    that is not digitally signed by Microsoft.
::  - Installs over what is already there (no uninstall, nothing else changed).
::  - Windows 7 SP1 and 8.1 get the last v14 build that supports them (14.29).
::  - Works on Windows 7 SP1, 8.1, 10, 11 and Windows Server 2008 R2 to 2025.
::
::  Options:  /quiet   no prompts (for scripts and deployment tools)
::            /check   download and verify only, install nothing
::  Exit code: 0 = done, 3010 = done but restart needed, 1 = something failed.
:: ============================================================================
setlocal EnableExtensions EnableDelayedExpansion
title All in One Visual C++ Runtimes Installer v2.0
:: Windows PowerShell must use its own default module path (a path inherited from PowerShell 7 stops it
:: loading the signature check).
set "PSModulePath="

set "QUIET=0"
set "CHECKONLY=0"
for %%A in (%*) do (
    if /i "%%~A"=="/quiet" set "QUIET=1"
    if /i "%%~A"=="/check" set "CHECKONLY=1"
)

:: ---- Administrator rights (not needed for /check) ---------------------------
if "%CHECKONLY%"=="0" (
    fltmc >nul 2>&1
    if errorlevel 1 (
        echo Requesting administrator rights...
        if "%~1"=="" (
            powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs" >nul 2>&1
        ) else (
            powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -ArgumentList '%*' -Verb RunAs" >nul 2>&1
        )
        if errorlevel 1 (
            echo.
            echo Could not get administrator rights. Right-click this file and choose
            echo "Run as administrator".
            if "%QUIET%"=="0" pause
            exit /b 1
        )
        exit /b 0
    )
)

:: ---- This PC: Windows build and architecture --------------------------------
set "BUILD=0"
for /f "tokens=3" %%V in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentBuildNumber 2^>nul ^| find "CurrentBuildNumber"') do set "BUILD=%%V"
set "ARCH=%PROCESSOR_ARCHITECTURE%"
if defined PROCESSOR_ARCHITEW6432 set "ARCH=%PROCESSOR_ARCHITEW6432%"

if %BUILD% LSS 7601 (
    echo This PC runs a Windows version older than Windows 7 SP1 ^(build %BUILD%^).
    echo Microsoft's current downloads need Windows 7 SP1 or later.
    if "%QUIET%"=="0" pause
    exit /b 1
)

set "WINNAME=Windows 7 / 8.1"
if %BUILD% GEQ 10240 set "WINNAME=Windows 10"
if %BUILD% GEQ 22000 set "WINNAME=Windows 11"

:: Which architectures to install.
::  x86 PC: x86 only.  x64 PC: x86 + x64.
::  ARM64 on Windows 11: x86 + x64 (the x64 v14 package also contains the ARM64 files).
::  ARM64 on Windows 10: x86 + ARM64 v14 (Windows 10 on ARM cannot run x64 programs).
set "DO_X86=1"
set "DO_X64=0"
set "DO_ARM64=0"
if /i "%ARCH%"=="AMD64" set "DO_X64=1"
if /i "%ARCH%"=="ARM64" (
    if %BUILD% GEQ 22000 (set "DO_X64=1") else (set "DO_ARM64=1")
)

:: Latest v14 (Windows 10 and later) or the last build for Windows 7 / 8.1.
if %BUILD% GEQ 10240 (
    set "V14_X86=https://aka.ms/vc14/vc_redist.x86.exe"
    set "V14_X64=https://aka.ms/vc14/vc_redist.x64.exe"
    set "V14_ARM64=https://aka.ms/vc14/vc_redist.arm64.exe"
    set "V14_NAME=latest v14 2015-2026"
) else (
    set "V14_X86=https://download.visualstudio.microsoft.com/download/pr/b929b7fe-5c89-4553-9abe-6324631dcc3a/4C6C420CF4CBF2C9C9ED476E96580AE92A97B2822C21329A2E49E8439AC5AD30/VC_redist.x86.exe"
    set "V14_X64=https://download.visualstudio.microsoft.com/download/pr/b929b7fe-5c89-4553-9abe-6324631dcc3a/296F96CD102250636BCD23AB6E6CF70935337B1BBB3507FE8521D8D9CFAA932F/VC_redist.x64.exe"
    set "V14_NAME=v14 14.29, last for Windows 7 and 8.1"
)

set "WORK=%TEMP%\itechtics-vc-runtimes-%RANDOM%%RANDOM%"
set "LOG=%TEMP%\itechtics-vc-runtimes.log"
md "%WORK%" >nul 2>&1
set /a OK=0, FAIL=0, REBOOT=0

echo.
echo  All in One Visual C++ Runtimes Installer v2.0 - itechtics.com
echo  -------------------------------------------------------------
echo  This PC: %WINNAME% (build %BUILD%), %ARCH%
if "%CHECKONLY%"=="1" echo  Mode: check only (download and verify, install nothing)
echo  Log: %LOG%
if "%CHECKONLY%"=="0" echo  Installing takes a few minutes. The last packages can take a while: please wait.
echo.
>>"%LOG%" echo.
>>"%LOG%" echo ==== %DATE% %TIME%  build %BUILD%  %ARCH%  check=%CHECKONLY%

:: ---- Packages, oldest first --------------------------------------------------
:: Arguments: label, URL, local file name, installer type (2005 / 2008 / 2010 / new).
if "%DO_X86%"=="1" call :package "Visual C++ 2005 x86" "https://download.microsoft.com/download/8/B/4/8B42259F-5D70-43F4-AC2E-4B208FD8D66A/vcredist_x86.EXE" "vc2005_x86.exe" 2005
if "%DO_X64%"=="1" call :package "Visual C++ 2005 x64" "https://download.microsoft.com/download/8/B/4/8B42259F-5D70-43F4-AC2E-4B208FD8D66A/vcredist_x64.EXE" "vc2005_x64.exe" 2005
if "%DO_X86%"=="1" call :package "Visual C++ 2008 x86" "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe" "vc2008_x86.exe" 2008
if "%DO_X64%"=="1" call :package "Visual C++ 2008 x64" "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x64.exe" "vc2008_x64.exe" 2008
if "%DO_X86%"=="1" call :package "Visual C++ 2010 x86" "https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x86.exe" "vc2010_x86.exe" 2010
if "%DO_X64%"=="1" call :package "Visual C++ 2010 x64" "https://download.microsoft.com/download/1/6/5/165255E7-1014-4D0A-B094-B6A430A6BFFC/vcredist_x64.exe" "vc2010_x64.exe" 2010
if "%DO_X86%"=="1" call :package "Visual C++ 2012 x86" "https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x86.exe" "vc2012_x86.exe" new
if "%DO_X64%"=="1" call :package "Visual C++ 2012 x64" "https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe" "vc2012_x64.exe" new
if "%DO_X86%"=="1" call :package "Visual C++ 2013 x86" "https://aka.ms/highdpimfc2013x86enu" "vc2013_x86.exe" new
if "%DO_X64%"=="1" call :package "Visual C++ 2013 x64" "https://aka.ms/highdpimfc2013x64enu" "vc2013_x64.exe" new
if "%DO_X86%"=="1" call :package "Visual C++ %V14_NAME% x86" "%V14_X86%" "vc14_x86.exe" new
if "%DO_X64%"=="1" call :package "Visual C++ %V14_NAME% x64" "%V14_X64%" "vc14_x64.exe" new
if "%DO_ARM64%"=="1" call :package "Visual C++ %V14_NAME% ARM64" "%V14_ARM64%" "vc14_arm64.exe" new

:: ---- Summary ------------------------------------------------------------------
rd /s /q "%WORK%" >nul 2>&1
echo.
echo  -------------------------------------------------------------
if "%CHECKONLY%"=="1" (
    echo  Verified: %OK%   Failed: %FAIL%
) else (
    echo  Installed or already up to date: %OK%   Failed: %FAIL%
)
>>"%LOG%" echo ==== done: ok=%OK% failed=%FAIL% reboot=%REBOOT%
set "RC=0"
if %REBOOT% GTR 0 (
    echo  Restart the PC to finish the installation.
    set "RC=3010"
)
if %FAIL% GTR 0 (
    echo  Some packages failed. Details: %LOG%
    echo  Fixes for common errors: https://www.itechtics.com/microsoft-visual-c-redistributable-versions-direct-download-links/#cant-install-visual-c-redistributable
    set "RC=1"
)
echo.
if "%QUIET%"=="0" pause
exit /b %RC%

:: ============================================================================
:package
::  %1 label  %2 URL  %3 file name  %4 installer type
set "LABEL=%~1"
set "FILE=%WORK%\%~3"
<nul set /p "=  !LABEL!: downloading... "
call :download "%~2" "%FILE%"
if errorlevel 1 (
    echo download FAILED
    >>"%LOG%" echo FAIL download  !LABEL!  %~2
    set /a FAIL+=1
    exit /b 1
)
<nul set /p "=checking... "
call :verify "%FILE%"
if errorlevel 1 (
    echo NOT signed by Microsoft, skipped
    >>"%LOG%" echo FAIL signature !LABEL!  %~2
    del /q "%FILE%" >nul 2>&1
    set /a FAIL+=1
    exit /b 1
)
if "%CHECKONLY%"=="1" (
    echo verified
    >>"%LOG%" echo OK   verified   !LABEL!
    set /a OK+=1
    exit /b 0
)
<nul set /p "=installing... "
if "%~4"=="2005" start "" /wait "%FILE%" /Q /C:"msiexec /i ""vcredist.msi"" /quiet"
if "%~4"=="2008" start "" /wait "%FILE%" /q
if "%~4"=="2010" start "" /wait "%FILE%" /q /norestart
if "%~4"=="new"  start "" /wait "%FILE%" /install /quiet /norestart
set "RCODE=%errorlevel%"
del /q "%FILE%" >nul 2>&1
if "%RCODE%"=="0"    (echo installed& set /a OK+=1& >>"%LOG%" echo OK   %RCODE%  !LABEL!& exit /b 0)
if "%RCODE%"=="1638" (echo already installed ^(same or newer^)& set /a OK+=1& >>"%LOG%" echo OK   %RCODE%  !LABEL!& exit /b 0)
if "%RCODE%"=="3010" (echo installed, restart needed& set /a OK+=1& set /a REBOOT+=1& >>"%LOG%" echo OK   %RCODE%  !LABEL!& exit /b 0)
if "%RCODE%"=="1641" (echo installed, restart needed& set /a OK+=1& set /a REBOOT+=1& >>"%LOG%" echo OK   %RCODE%  !LABEL!& exit /b 0)
echo FAILED ^(error %RCODE%^)
>>"%LOG%" echo FAIL %RCODE%  !LABEL!
set /a FAIL+=1
exit /b 1

:: ============================================================================
:download
::  %1 URL  %2 target file. Tries curl (Windows 10+), then PowerShell with TLS 1.2, then BITS.
if exist "%~2" del /q "%~2" >nul 2>&1
where curl.exe >nul 2>&1
if not errorlevel 1 (
    curl.exe -fsSL --retry 3 --retry-delay 2 -o "%~2" "%~1" >nul 2>&1
    if exist "%~2" exit /b 0
)
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 3072 } catch {}; try { (New-Object Net.WebClient).DownloadFile('%~1', '%~2'); exit 0 } catch { exit 1 }" >nul 2>&1
if exist "%~2" exit /b 0
bitsadmin /transfer itechtics-vc /download /priority foreground "%~1" "%~2" >nul 2>&1
if exist "%~2" exit /b 0
exit /b 1

:: ============================================================================
:verify
::  %1 file. Valid Authenticode signature from Microsoft Corporation, or no run.
powershell -NoProfile -ExecutionPolicy Bypass -Command "$s = Get-AuthenticodeSignature -FilePath '%~1'; if ($s.Status -eq 'Valid' -and $s.SignerCertificate.Subject -match 'O=Microsoft Corporation') { exit 0 } else { exit 1 }" >nul 2>&1
exit /b %errorlevel%
