Nick W.
Nick W.

Reputation: 1614

Batch File Loop Issue

Ok, I have spent the better half of the last like 6 hours trying to get this to work and I'm nearly pulling my hair out. If someone could help be figure this out I would be eternally grateful.

@ECHO on
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
CALL:MicrsoftInstaller-IntegrityCheck "x86" "KB978601" "2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu" "/quiet /norestart"
pause

:MicrsoftInstaller-IntegrityCheck
SET MSCheck="%~1"
IF "%~1" NEQ "" (
    IF /I %MSCheck:~1,1%=="/"       SET MSI_Switch=%MSCheck%
    IF /I %MSCheck:~1,2%=="20"      SET MSI_Path=%MSCheck%
    IF /I %MSCheck:~1,20%=="KB"     SET MSI_KB=%MSCheck%
    IF /I %MSCheck%=="x64"          SET MSI_Architecture=x64
    IF /I %MSCheck%=="x86"          SET MSI_Architecture=x86    
       SHIFT
       GOTO:MicrsoftInstaller-IntegrityCheck
)
ECHO KB: %MSI_KB%
ECHO Switches: %MSI_Switch%
ECHO Path: %MSI_Path%
ECHO Architecture: %MSI_Architecture%
GOTO:EOF

Upvotes: 1

Views: 447

Answers (1)

marapet
marapet

Reputation: 56456

Some small changes were necessary to make it work:

@ECHO off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
CALL:MicrsoftInstaller-IntegrityCheck "x86" "KB978601" "2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu" "/quiet /norestart"
pause

:MicrsoftInstaller-IntegrityCheck
SET MSCheck="%~1"
IF "%~1" EQU "" GOTO Part2

IF /I "%MSCHECK:~1,1%"=="/"       SET MSI_Switch=%MSCheck%
IF /I "%MSCheck:~1,2%"=="20"      SET MSI_Path=%MSCheck%
IF /I "%MSCheck:~1,2%"=="KB"     SET MSI_KB=%MSCheck%
IF /I %MSCheck%=="x64"          SET MSI_Architecture=x64
IF /I %MSCheck%=="x86"          SET MSI_Architecture=x86    
SHIFT
GOTO:MicrsoftInstaller-IntegrityCheck

:Part2
ECHO KB: %MSI_KB%
ECHO Switches: %MSI_Switch%
ECHO Path: %MSI_Path%
ECHO Architecture: %MSI_Architecture%
GOTO:EOF

First I added some double-quotes in your if statements.

The second problem is linked to how Batch interprets code: line after line. When it came to your line IF "%~1" NEQ "" ( it tried to interpret the following code block even when the condition was not satisfied (=no more argument to check). But with MSCHECK equal to "", the first if statement became IF /I """=="/" which resulted in an error.

Upvotes: 1

Related Questions