Batch file to update iTunes depending on version

743 Views Asked by At

Trying to create a batch file that will check the iTunes version and then will update if the version is lower than the version listed in the script.

The issue I'm having is what is the best way to get the value from the registry key to my IF value.

I have looked around on Google a bit and can't find something that matches what I want to do.

::Finds the value of the Version key
REG QUERY "HKLM\SOFTWARE\Apple Computer, Inc.\iTunes" /v Version

This is where I am stuck. How do I use the value from Version? Do I need to use a FOR loop for this? I have tried playing with it but not su

::If the version matches the number below iTunes is up to date
IF Version==12.5.4.42 @echo Up to date!  && goto end

::If the version is not equal to the number below 
IF NOT Version==12.5.4.42 && goto install

::Installs the current version from the repository
:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi" 

msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi" 

msiexec.exe /qn /norestart /i "%~dp0itunes.msi"

echo Cleaning Up Installation

del C:\Users\Public\Desktop\iTunes.lnk

:end
exit

I feel like a tool that I can't get this figured out. Haven't dealt with FOR statements before. Apologies in advance for my stupidity.

2

There are 2 best solutions below

5
On

One specific problem with your script is that you've got an extra && in this line:

IF NOT Version==12.5.4.42 && goto install

Temporarily remarking out @echo off can help you find these simple syntax errors. And as Magoo points out, Version is a string that will never equal 12.5.4.42. Variables in batch are surrounded by % when you want to evaluate them (or sometimes !).

More generally, when comparing version numbers, it'd be better to use a language that can objectify the version number and can understand major.minor.build.revision. You don't want to trigger the install if the installed version is, for example, 12.10.0.0. Comparing that against 12.5.4.42 in batch would trigger the install. Even though 12.10.x.x is numerically greater than 12.5.x.x, it is alphabetically less, and is treated as the lower value by if comparisons.

As an illustration, from a cmd console, enter this and see what happens:

if 12.10.0.0 leq 12.5.4.42 @echo triggered!

I'd use PowerShell for the heavy lifting here. Here's an illustration using a Batch + PowerShell hybrid script. I haven't tested it since I don't have iTunes installed, so you might need to salt to taste.

<# : batch portion (begin multiline PowerShell comment block)
@echo off & setlocal

set "installer_version=12.5.4.42"

powershell -noprofile "iex (${%~f0} | out-string)" && (
    echo Up to date!
    goto :EOF
)

:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi" 
msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi" 
msiexec.exe /qn /norestart /i "%~dp0itunes.msi"
echo Cleaning Up Installation
del C:\Users\Public\Desktop\iTunes.lnk
goto :EOF

: end batch / begin PowerShell hybrid code #>

$regkey = "HKLM:\SOFTWARE\Apple Computer, Inc.\iTunes"
$installed = (gp $regkey Version -EA SilentlyContinue).Version
if (-not $installed) {
    "iTunes not installed."
    exit 1
}

# exits 0 if true, 1 if false (-le means <=)
exit !([version]$env:installer_version -le [version]$installed)

To answer the question you asked, though, how to capture the output of reg or any other command, use a for /F loop. See for /? in a cmd console for full details.

0
On
IF Version==12.5.4.42 @echo Up to date!  && goto end

The string Version is never going to be equal to the string 12.5.4.42. You need the contents of Version, so the code should be

IF %Version%==12.5.4.42 @echo Up to date!&goto end

(a single & concatenates commands)

The following if is superfluous. To reach that statement, the version must be not-12.5.4.42 otherwise execution would have been transferred to :end

BTW, goto :eof where the colon in :eof is required means 'go to physical end-of-file'.