jaclaz Posted May 8, 2011 Posted May 8, 2011 (edited) A better source is actual dedicated sites:http://ss64.com/nt/http://www.robvanderwoude.com/batchfiles.phpYzöwl, which is our resident "batch master" has a "quirk" to writing very good snippets/scripts in the most simplified (which sometimes means complex ) way ).This:@ECHO OFF&SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION FOR /R %%# IN (*-KB*.MSU) DO (CALL :SUB %%~n# ECHO= INSTALLING KB!_!&ECHO=WUSA "%%#" /QUIET /NORESTART) PAUSE&GOTO :EOF :SUB SET "_=%*" FOR /F "DELIMS=-" %%$ IN ("%_:*-KB=%") DO SET "_=%%$"Can be re-written as:@ECHO OFFSETLOCAL ENABLEEXTENSIONS SETLOCAL ENABLEDELAYEDEXPANSION FOR /R %%A IN (*-KB*.MSU) DO (CALL :SUB %%~nAECHO INSTALLING KB!KB_number!ECHO=WUSA "%%A" /QUIET /NORESTART) PAUSEGOTO :EOF :SUB SET "KB_number=%*" FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B"Which may be more readable. jaclaz Edited May 8, 2011 by jaclaz
myselfidem Posted May 8, 2011 Posted May 8, 2011 (edited) Thanks Yzöwl and jaclaz! I've made some changes and the batch file works fine for me like below!After testing I see this batch file works fine, if I set the batch file inside a folder (example: C:\Post_SP1\) and the batch searching inside a subfolder (example: C:\Post_SP1\Updates);or if it is inside the same folder where the updates are located (example: C:\Post_SP1\Updates\).The batch file is inside C:\Post_SP1\ or inside C:\Post_SP1\Updates and is working fine (outside or inside the folder).Works also fine if I launch the batch file on C:\ partition!@ECHO OFFSETLOCAL ENABLEEXTENSIONS SETLOCAL ENABLEDELAYEDEXPANSION FOR /R %%A IN (*-KB*.MSU) DO (CALL :SUB %%~nAECHO INSTALLING KB!KB_number!TIMEOUT /t 3 >NUL C:\Windows\System32\wusa.exe %%A /quiet /norestart) ECHO == Press any key to restart ==&PAUSE>NULshutdown.exe /r /t 0GOTO :EOF :SUB SET "KB_number=%*" FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B"Output command window------------------------------------------------------------INSTALLING KB2425227INSTALLING KB2479943INSTALLING KB2491683INSTALLING KB2503658INSTALLING KB2506212INSTALLING KB2506223INSTALLING KB2507618INSTALLING KB2508272INSTALLING KB2508429INSTALLING KB2509553INSTALLING KB2511455== Press any key to restart ==------------------------------------------------------------ Edited May 8, 2011 by myselfidem
Yzöwl Posted May 8, 2011 Posted May 8, 2011 Just thought I'd add a note about the use of the /R switch with FOR in this case.You are recursing the directory rooted at the current directory, not necessarily that of the batch files location. If you wanted to make sure of that I'd suggest that you think about using either:FOR /R C:\Post_SP1 %%A IN (orFOR /R %~dp0 %%A IN (You may of course need to ensure that paths with spaces are catered for too!
myselfidem Posted May 8, 2011 Posted May 8, 2011 (edited) Thanks Yzöwl to point me about this question!Could you tell me if the batch file is correct like this.Works for me, even if the folder has a space in its name.@ECHO OFFSETLOCAL ENABLEEXTENSIONS SETLOCAL ENABLEDELAYEDEXPANSION FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (CALL :SUB %%~nAECHO INSTALLING KB!KB_number!TIMEOUT /t 3 >NUL C:\Windows\System32\wusa "%%A" /quiet /norestart) ECHO == Press any key to restart ==&PAUSE>NULshutdown.exe /r /t 0GOTO :EOF :SUB SET "KB_number=%*" FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B" Edited May 8, 2011 by myselfidem
Yzöwl Posted May 8, 2011 Posted May 8, 2011 That is correct.Do bear in mind that WUSA.EXE is located within %PATH% as is SHUTDOWN.EXE and that executables located within the path do not require their path prepending to the command. You have done so with one and not the other, whilst both are correct it seems pointless to include that which isn't required.You can also simplify lines 2 & 3 by merging them into one thus@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONFOR /R "%~dp0" %%A IN (*-KB*.MSU) DO ( CALL :SUB %%~nA ECHO= Installing KB!KB_NUM! >NUL TIMEOUT /t 3 WUSA "%%A" /quiet /norestart)ECHO= == Press any key to restart ==>NUL PAUSESHUTDOWN.EXE /r /t 0GOTO :EOF:SUBSET "KB_NUM=%*"FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B" 1
myselfidem Posted May 8, 2011 Posted May 8, 2011 (edited) Many thanks YzöwlReally nice and awesome! But, I must admit it is hard for me, because I'm a newbe with batch files.However It's very interresting to learn how that works.Thanks again for your time and the very useful informations! Edited May 8, 2011 by myselfidem
centi50 Posted May 8, 2011 Author Posted May 8, 2011 What if am to install Win Xp Updates, is there something to edit in the Batch code Being Provided here
Yzöwl Posted May 9, 2011 Posted May 9, 2011 For windows xp updates, you can probably get away with simply changing MSU to EXE and removing WUSA.@ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION FOR /R "%~dp0" %%A IN (*-KB*.EXE) DO ( CALL :SUB %%~nA ECHO= Installing KB!KB_NUM! >NUL PING -n 4 127.0.0.1 "%%A" /quiet /norestart) ECHO= == Press any key to restart == >NUL PAUSE SHUTDOWN.EXE /r /t 0 GOTO :EOF :SUB SET "KB_NUM=%*" FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"
centi50 Posted May 9, 2011 Author Posted May 9, 2011 Thanks Yzöwl Thanking Everyone for helping me outPEACE AND LOVE TO ALL
centi50 Posted June 22, 2011 Author Posted June 22, 2011 (edited) Please can anyone Modify this batch Script to include the Installation of software updates of the type SFXCAB.EXE as in Silverlight and type WEXTRACT.EXE as in Microsoft Visual C++ 2005 Redisributable package.The above script will Search for the updates in The form of KB in the folder and subfolders is Placed in.The scenario that i was faced with was after Manual downloading the windows updates which obviously were like windowsxp-kb2476490-x86-enu_1545ba40647300fc0cde0c3e4c8e307e7d77d45dndp40-kb2518870-x86_42ed6547df9927704552b65505ed7dd76b363be6and many others like the above two, i also included the software optional updates like type SFXCAB.EXE as in Silverlight and type WEXTRACT.EXE as in Microsoft Visual C++ 2005 Redisributable package. the above script did not install the software optional updates.Therefore Please can Anyone modify the script to make it possible also to install software optional updates. That is if it is Possible.your Help will be highly appreciatedThanking All In Advance Edited June 23, 2011 by Yzöwl Merged New Topic with its directly related thread
xranger Posted January 16, 2012 Posted January 16, 2012 hi,Yzowl. I am a newbe here..your code is awesome thanks a lot.But I just got a little problem,when I tested this batch by choosing "run as administrator"(windows7 x64) in a folder with all MSU and EXE update files, .bat processed and listed them,but in fact none of them was actuelly installed. Then I tried to add "cd /d %~dp0" to change the path at the beginning before your code, and it worked finally. But in your code these is already "FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (" , looks like this parte doesn't work for me, I dont understand why, could you please explain?PS: your second code for .exe file updating woks well.That is correct.Do bear in mind that WUSA.EXE is located within %PATH% as is SHUTDOWN.EXE and that executables located within the path do not require their path prepending to the command. You have done so with one and not the other, whilst both are correct it seems pointless to include that which isn't required.You can also simplify lines 2 & 3 by merging them into one thus@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONFOR /R "%~dp0" %%A IN (*-KB*.MSU) DO ( CALL :SUB %%~nA ECHO= Installing KB!KB_NUM! >NUL TIMEOUT /t 3 WUSA "%%A" /quiet /norestart)ECHO= == Press any key to restart ==>NUL PAUSESHUTDOWN.EXE /r /t 0GOTO :EOF:SUBSET "KB_NUM=%*"FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"
Yzöwl Posted January 16, 2012 Posted January 16, 2012 If all of the updates are located within the same directory as the batch file then you don't need the recursion and can indeed navigate to the source directory.@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONPUSHD %~dp0FOR %%A IN (*-KB*.MSU) DO ( CALL :SUB %%~nA ECHO= Installing KB!KB_NUM! >NUL TIMEOUT /t 3 WUSA "%%~fA" /quiet /norestart)ECHO= == Press any key to restart ==>NUL PAUSESHUTDOWN.EXE /r /t 0GOTO :EOF:SUBSET "KB_NUM=%*"FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"
xranger Posted January 17, 2012 Posted January 17, 2012 Thank you,Yzowl..You 're right..I retested the batch file, it works well now..that's odd,maybe I forgot restarting my system after patching Sp1 Anyway.thank again
MrAnderson Posted March 4, 2012 Posted March 4, 2012 @ECHO OFF:: Check for permissions>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system":: If error flag set, we do not have admin.if '%errorlevel%' NEQ '0' (Echo Requesting administrative privileges...goto UACPrompt) else ( goto gotAdmin ):UACPromptEcho Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"Echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs""%temp%\getadmin.vbs"Exit /B:gotAdminif exist "%temp%\getadmin.vbs" ( Del "%temp%\getadmin.vbs" )Pushd "%CD%"CD /D "%~dp0":--------------------------------------SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET ERRORLEVEL=SET ERRNUM=FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO ( CALL :SUB %%~nA ECHO= Installing KB!KB_NUM! >NUL TIMEOUT /t 3 SET ERRORLEVEL= WUSA "%%A" /quiet /norestart SET ERRNUM=!ERRORLEVEL! IF !ERRNUM! EQU 3010 ( ECHO= == SUCCESS ^(RebootRequired^) ^[!ERRNUM!^] == ) ELSE ( IF ERRORLEVEL 1 ( ECHO= == FAILED ^[!ERRNUM!^] == ) ELSE ( ECHO= == SUCCESS ^[!ERRNUM!^] == ) ))CHOICE /C YN /M "Close all programs and press Y to restart or N to continue."IF ERRORLEVEL 2 GOTO continueIF ERRORLEVEL 1 GOTO restartGOTO end:continue GOTO end:restart ECHO= == Press any key to restart == >NUL PAUSE SHUTDOWN.EXE /r /t 0:endGOTO :EOF:SUBSET "KB_NUM=%*"FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"REM http://www.msfn.org/board/topic/152020-batch-script-for-windows-software-updates-installation/page__view__findpost__p__964942REM http://www.windows-commandline.com/2011/10/choice-command.htmlREM http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspxREM http://ss64.com/nt/delayedexpansion.htmlREM http://ss64.com/nt/runas.html++++++++ VBScript Alternative+++++++'http://ccmexec.com/2012/02/installing-multiple-windows-7-hotfixes-msu-with-sccm/'http://www.insidethe.com/blog/2009/12/how-to-launch-a-wsh-vbscript-as-administrator-in-windows-7-and-vista/mytitle="MSU Installer"mybuttons=65 ' vbOKCancel + vbInformationIf WScript.Arguments.Named.Exists("elevated") = False Then 'Launch the script again as administrator CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1 WScript.QuitElse 'Change the working directory from the system32 folder back to the script's folder. Set oShell = CreateObject("WScript.Shell") oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) MsgBox "Now running with elevated permissions",0,mytitleEnd IfDim objfso, objShellDim folder, files, sFolder, folderidx, Iretval, returnSet objfso = CreateObject("Scripting.FileSystemObject")Set objShell = CreateObject("Wscript.Shell")sFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))Set folder = objfso.GetFolder(sFolder)Set files = folder.Filessomething=0 ' there is nothing to doFor each folderIdx In filesIf Ucase(Right(folderIdx.name,3)) = "MSU" thensomething=1 ' there is something to do.wscript.echo "wusa.exe " & sfolder & folderidx.name & " /quiet /norestart"'wscript.echo "wusa.exe " & sfolder & folderidx.nameiretval=objShell.Run ("wusa.exe " & sfolder & folderidx.name & " /quiet /norestart", 1, True)'iretval=objShell.Run ("wusa.exe " & sfolder & folderidx.name, 1, True)If (iRetVal = 0) or (iRetVal = 3010) then'wscript.echo folderidx.name & " Success"myprompt=folderidx.name & " Success " & "[" & iRetVal & "]"Else'wscript.echo folderidx.name & " Failed"myprompt=folderidx.name & " Failed " & "[" & iRetVal & "]"End If devam=MsgBox(myprompt,mybuttons,mytitle) If (devam = 2) or (devam = 3) or (devam = 7) Then 'Cancel/Abort/No was clicked wscript.quit(1) End IfEnd IfNextIf (something = 0) Then MsgBox "Nothing to do.",64,mytitleElse MsgBox "Done.",64,mytitle restartla=MsgBox("Close all programs and press Yes to restart or No to continue.",36,mytitle) If (restartla = 6) Then Set WSHShell = WScript.CreateObject("WScript.Shell") WshShell.Run "%SYSTEMROOT%\system32\shutdown.exe /r /t 0" End IfEnd IfInstallMSU.zip
dsrtFx Posted July 12, 2012 Posted July 12, 2012 Yzowl,What to do to run that batch file for a multiple computers? What commands need to be added?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now