4
\$\begingroup\$

I would like to know how to simplify and improve this script, which should:

  • Check to determine if the Central Process is 32 or 64 bit
  • Determine if, specifically, Java 1.8 is installed on the computer
    • If not, download and install it depending on the CP
  • Set %JAVA_HOME% as an environmental variable
  • Download, unzip, move to C:\Gradle, and set the latest version of Gradle 4.4 as an environmental variable
  • Determine if Git is installed on the computer
    • If not, download and install it depending on the CP

GitHub Repo

@echo OFF

reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F64180151F0}" > nul
if errorlevel 1 goto installJavaByCP
if errorlevel 0 goto skipJavaInstallation

:installJavaByCP
   if %OS%==32BIT goto installJava32
   if %OS%==64BIT goto installJava64

:installJava32
   echo Downloading and Installing Java 32BIT
   START /WAIT bitsadmin.exe /transfer "Downloading Java 1.8" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-windows-i586.exe %HOMEPATH%\Downloads\
   START /WAIT %HOMEDRIVE%\Downloads\jdk-8u151-windows-i586.exe
   setx %JAVA_HOME% "C:\Program Files (x86)\Java\jdk1.8.0_151" /m
   goto setGradle

:installJava64
   echo Downloading and Installing Java 64BIT
   START /WAIT bitsadmin.exe /transfer "Downloading Java 1.8" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-windows-x64.exe %HOMEPATH%\Downloads\
   START /WAIT %HOMEDRIVE%\Downloads\jdk-8u151-windows-x64.exe
   setx %JAVA_HOME% "C:\Program Files\Java\jdk1.8.0_151" /m
   goto setGradle

:skipJavaInstallation
   echo Application Found, Canceling Installation
   goto setGradle

:setGradle
   echo Downloads and Properly Sets the Latest Version of Gradle
   START /WAIT bitsadmin.exe /transfer "Downloading Latest Version of Gradle" https://services.gradle.org/distributions/gradle-4.4-bin.zip %HOMEPATH%\Downloads\
   runas /savecred /profile /user:Administrator unzipGradle.bat
   if not exist "C:\Gradle\" mkdir C:\Gradle"
   move %HOMEPATH%\Downloads\gradle-4.4 C:\Gradle\
   SET GRADLE=";C:\Gradle\gradle-4.4\bin"
   goto gitChecker

:gitChecker
   reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1" > nul
   if errorlevel 1 goto installGitByCP
   if errorlevel 0 goto skipGitInstallation

:installGitByCP
   if %OS%==32BIT goto installGit32
   if %OS%==64BIT goto installGit64

:installGit32
   echo Downloading and Installing Git - 32
   START /WAIT bitsadmin.exe /transfer "Downloading Latest Version of Git" https://git-scm.com/download/wins %HOMEPATH%\Downloads\
   START /WAIT %HOMEPATH%\Downloads\Git-2.15.1.2-32-bit.exe
   goto exit

:installGit64
   echo Downloading and Installing Git - 64
   START /WAIT bitsadmin.exe /transfer "Downloading Latest Version of Git" https://git-scm.com/download/wins %HOMEPATH%\Downloads\
   START /WAIT %HOMEPATH%\Downloads\Git-2.15.1.2-64-bit.exe
   goto exit

:skipGitInstallation
   echo Application Found, Canceling Installation
   goto exit

:exit
   echo All Necessary Files Installed and Set, Install IntelliJ
   echo Script is Finished, Exiting
   exit

I am no aficionado on Windows Batch, and as such, I am open to all suggestions on how to improve the script and make it more efficient.

\$\endgroup\$
  • 1
    \$\begingroup\$ The script is called using this batch script: runas /savecred /profile /user:Administrator installerRun.bat And the unzipGradle.bat file in setGradle is this: unzipGradle.bat unzip -source %HOMEPATH%\Downloads\gradle-4.4.zip -destination C:\Gradle -keep yes -force no \$\endgroup\$ – PB020 Dec 17 '17 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.