Batch Script to Install Downloaded Updates

Discussion in 'Windows 8' started by ColdZero, Nov 19, 2014.

  1. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    #1 ColdZero, Nov 19, 2014
    Last edited: Nov 24, 2014

    Attached Files:

  2. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    #2 ColdZero, Nov 19, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    You can learn a little bit of batch scripting from this batch: ex:

    You can use the cmd command: "openfiles" to check if you are Administrator.

    The Command:
    Code:
    Powershell -Command "& {dir | Unblock-File}"
    
    Unblocks all the downloaded files from internet or from another computer (it prevents the warning thus the script won't pause)

    If you check the script you will learn how to make the script count files (math operations) "set /A totalexe = totalexe + 1"
    (To make math works on batch scripts you need enable "setlocal EnableDelayedExpansion")

    Take a look, you are free to use or improve the script.
     
  3. Chibi ANUBIS

    Chibi ANUBIS MDL Chibi Developer

    Apr 28, 2014
    1,238
    912
    60
    Thanks for your script !
    I create a media with rollup and I try to integrate in WIM also
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. LastWords

    LastWords MDL Member

    Aug 29, 2013
    101
    127
    10
    Thanks for script & small tutorial...

    Need one help as all downloaded files are not in one folder, then what to add in script so that it search & execute files inside folders inside root folder...
     
  5. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    #5 ColdZero, Nov 21, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    It is easier to search for *.msu and move or copy all the patches to a single folder.

    If you intend to change the script like you told me then it will change 30% of the script.
    Some parts will look like this:
    Code:
    for /D %%d in (*) do (
        cd %%d
        call :Function
        cd ..
    )
    
    Try this code and let me know:
    FileName: Update-Recursive.cmd
    Code:
    @echo off
    setlocal EnableDelayedExpansion
    
    cls
    title Windows 8.1 Offline Updater
    color 1F
    
    set BATDIR1=%~dp0
    cd /d %BATDIR1%
    
    :Check-Admin
    openfiles >nul 2>&1
    if %errorlevel% NEQ 0 goto :required
    
    :UnblockFiles
    cls
    echo Unblocking Files
    echo+
    echo Executing Powershell...
    echo+
    for /D %%d in (*) do (
        cd %%d
    Powershell -Command "& {dir | Unblock-File}"
        cd ..
    )
    title Windows 8.1 Offline Updater
    echo+
    
    :Counters
    set /A countermsu=0
    set /A counterexe=0
    set /A totalmsu=0
    set /A totalexe=0
    
    for /D %%d in (*) do (
        cd %%d
    @for %%V in (*.msu) do (
    set /A totalmsu = totalmsu + 1
    )
    @for %%V in (*.exe) do (
    set /A totalexe = totalexe + 1
    )
        cd ..
    )
    
    :Menu
    cls
    Echo+
    Echo -------------------------------------------------------------------------------
    Echo    Windows 8.1 Off-Line Updates                       Package:  OEM
    Echo -------------------------------------------------------------------------------
    Echo+
    echo Hello %USERNAME%, what do you want to do?
    echo+
    echo    A) Install Critical Updates (MSU: %totalmsu%) (EXE: %totalexe%)
    echo    B) View Installed Updates
    echo    Q) Exit
    echo+
    
    set /p userinp= ^> Select Option : 
    set userinp=%userinp:~0,1%
    if /i "%userinp%"=="Q" exit
    if /i "%userinp%"=="A" goto :Patches_Install
    if /i "%userinp%"=="B" goto :Patches_List
    goto :Menu
    
    :Patches_List
    If exist patchlist.txt del patchlist.txt
    wmic qfe get hotfixid >patchlist.txt
    start patchlist.txt
    goto :Menu
    exit
    
    :Patches_Install
    cls
    Echo+
    Echo -------------------------------------------------------------------------------
    Echo    Windows 8.1 Off-Line Updates                       Package:  OEM
    Echo -------------------------------------------------------------------------------
    Echo+
    echo+
    echo Installing OEM Patches...
    echo+
    
    for /D %%d in (*) do (
        cd %%d
    echo Folder %%d
    @for %%V in (*.exe) do (
    set /A counterexe = counterexe + 1
    echo Installing !counterexe! of %totalexe%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
    
    @for %%V in (*.msu) do (
    set /A countermsu = countermsu + 1
    echo Installing !countermsu! of %totalmsu%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
        cd ..
    echo+
    )
    
    echo+
    echo ***********
    echo  Complete!
    echo ***********
    echo+
    Pause
    cd..
    Exit
    
    :required
    cls
    color 0f
    echo Run the script as Administrator
    echo+
    echo Correr el script como Administrador
    echo+
    pause
    exit
    
    

    This code will go +1 level only:
    Root Folder --> SubFolder --> Files ( *.msu or *.exe )
     
  6. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #6 s1ave77, Nov 21, 2014
    Last edited by a moderator: Apr 20, 2017
    Afaik

    Code:
    for /r "path\to\folder" %%V in (*.msu) do (
    )
    should recurse through all sub-directories :g:.


    EDIT: can confirm it does indeed :D.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    @s1ave77
    Yep, that will work too and goes further levels.
     
  8. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #8 s1ave77, Nov 21, 2014
    Last edited by a moderator: Apr 20, 2017
    Just checked:
    where
    Code:
    for %%V in (*.msu) do (
    )
    checks the directory only.
    Code:
    for /r %%V in (*.msu) do (
    )
    recurses all sub-directories.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    #9 ColdZero, Nov 22, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I must go now... :p (Will return on sunday) (small trip.)
    @s1ave77
    Can you help integrating the recursive loop in the main script?

    It has to work fully like this:
    * The PowerShell File Unblock function in each folder
    * The *.msu and *.exe file count
    * The patches installation (obviously)

    Thanks :cool:

    Another thing:
    Code:
    for /r "%BATDIR1%" %%d in (.) do (
        cd %%d
    Powershell -Command "& {dir | Unblock-File}"
        cd ..
    )
    
    I did not try this... but this should execute the unblock files in each directory.
    am i correct?
     
  10. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    #10 s1ave77, Nov 22, 2014
    Last edited by a moderator: Apr 20, 2017
    Hmm, if i got the challenge correctly it only needs to add the /r switch to the original code. As the script is copied to the main upadate folder, there's no need to specify any pathes additionally:

    Code:
    :Patches_Install
    cls
    Echo+
    Echo -------------------------------------------------------------------------------
    Echo    Windows 8.1 Off-Line Updates                       Package:  OEM
    Echo -------------------------------------------------------------------------------
    Echo+
    echo+
    echo Installing OEM Patches...
    echo+
    
    @for /r %%V in (*.exe) do (
    set /A counterexe = counterexe + 1
    echo Installing !counterexe! of %totalexe%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
    
    @for /r %%V in (*.msu) do (
    set /A countermsu = countermsu + 1
    echo Installing !countermsu! of %totalmsu%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
    Should be indeed that easy. Now it recurses the update folder when installing (updates only need to be in any sub-folder beneath) ... rest of the code doesn't change.


    ================================
    EDIT: OK, not sure whether powershell recurses ... any clue?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. Tito

    Tito Super Mod / Adviser
    Staff Member

    Nov 30, 2009
    18,681
    18,589
    340
  12. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. LastWords

    LastWords MDL Member

    Aug 29, 2013
    101
    127
    10
    #13 LastWords, Nov 22, 2014
    Last edited by a moderator: Apr 20, 2017
  14. LastWords

    LastWords MDL Member

    Aug 29, 2013
    101
    127
    10
    #14 LastWords, Nov 22, 2014
    Last edited by a moderator: Apr 20, 2017
    With following changes script is working fine. Tested for unblock & patch install....

    Thank you ColdZero & s1ave77....

    Code:
    @echo off
    setlocal EnableDelayedExpansioncls
    title Windows 8.1 Offline Updater
    color 1F
    set BATDIR1=%~dp0
    cd /d %BATDIR1%
    :Check-Admin
    openfiles >nul 2>&1
    if %errorlevel% NEQ 0 goto :required
    :UnblockFiles
    cls
    echo Unblocking Files
    echo+
    echo Executing Powershell...
    echo+
    Powershell -Command "& {ls -r | unblock-file}"
    title Windows 8 Offline Updater
    echo+
    :Counters
    set /A countermsu=0
    set /A counterexe=0
    set /A totalmsu=0
    set /A totalexe=0
    @for %%V in (*.msu) do (
    set /A totalmsu = totalmsu + 1
    )
    @for %%V in (*.exe) do (
    set /A totalexe = totalexe + 1
    )
    :Menu
    cls
    Echo+
    Echo -------------------------------------------------------------------------------
    Echo    Windows 8.1 Off-Line Updates                       Package:  OEM
    Echo -------------------------------------------------------------------------------
    Echo+
    echo Hello %USERNAME%, what do you want to do?
    echo+
    echo    A) Install Critical Updates (MSU: %totalmsu%) (EXE: %totalexe%)
    echo    B) View Installed Updates
    echo    Q) Exit
    echo+
    set /p userinp= ^> Select Option : 
    set userinp=%userinp:~0,1%
    if /i "%userinp%"=="Q" exit
    if /i "%userinp%"=="A" goto :Patches_Install
    if /i "%userinp%"=="B" goto :Patches_List
    goto :Menu
    :Patches_List
    If exist patchlist.txt del patchlist.txt
    wmic qfe get hotfixid >patchlist.txt
    start patchlist.txt
    goto :Menu
    exit
     
    :Patches_Install
    cls
    Echo+
    Echo -------------------------------------------------------------------------------
    Echo    Windows 8.1 Off-Line Updates                       Package:  OEM
    Echo -------------------------------------------------------------------------------
    Echo+
    echo+
    echo Installing OEM Patches...
    echo+
    @for /r %%V in (*.exe) do (
    set /A counterexe = counterexe + 1
    echo Installing !counterexe! of %totalexe%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
    @for /r %%V in (*.msu) do (
    set /A countermsu = countermsu + 1
    echo Installing !countermsu! of %totalmsu%
    echo %%V
    %%V /quiet /norestart
    If %errorlevel% NEQ 0 echo Warning: Minor error! "dont worry"
    echo+
    )
    echo+
    echo ***********
    echo  Complete!
    echo ***********
    echo+
    Pause
    cd..
    Exit
    
    :required
    cls
    color 0f
    echo Run the script as Administrator
    echo+
    echo Correr el script como Administrador
    echo+
    pause
    exit
    
    
     
  15. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    #15 ColdZero, Nov 22, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    You missed "/r" in :Counters section
    Anyway thanks. :cool:
    Updating main post.
     
  16. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    All the scripts works, thanks guys!

    I have a minor minor problem.
    On file "Update 2.2 - Recursive Deep.cmd"
    When the script goes thru the *.msu or *.exe it echoes the "+" instead of a blank line.

    any ideas why the script does that?

    PostNote: I am borrowing a laptop. :D
     
  17. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,226
    84,915
    340
    You could use echo. instead of echo+

    not tested though, i don't have un-installed updates :D
     
  18. ColdZero

    ColdZero MDL Android 17

    Nov 9, 2009
    698
    3,169
    30
    it echoes the .
     
  19. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    I normally use echo: for empty output lines :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,226
    84,915
    340