Jump to content

Recommended Posts

As the owner buys the new components. i do the assembly , windows installation etc do basicly im creating a script so that i can just copy the folder over to the pc and run the script.

My suggestion is to create a simple script which determines the OS and run that first to call one of a series of scripts designed for the resultant version.

e.g.

@ECHO OFF & SETLOCAL ENABLEEXTENSIONSSET "_OSV="FOR /F "TOKENS=1* DELIMS=[" %%A IN ('VER') DO (SET "_OSV=%%B"	CALL SET "_OSV=%%_OSV:* =%%" & CALL SET "_OSV=%%_OSV:~,3%%")IF %_OSV% LSS 5.1 (GOTO :EOF) ELSE (	IF %_OSV% LSS 6 (GOTO XP2K3) ELSE (		IF %_OSV% LSS 6.1 (GOTO VST2K8) ELSE (			IF %_OSV% LSS 6.2 (GOTO W72K8R2) ELSE (				IF %_OSV% LSS 6.3 (GOTO W82K12)))))Win_81.cmdGOTO :EOF:W82K12Win_8_2K12.cmdGOTO :EOF:W72K8R2Win_7_2K8R2.cmdGOTO :EOF:VST2K8Win_Vista_2K8.cmdGOTO :EOF:XP2K3Win_XP_2K3.cmd
Link to comment
Share on other sites


And I think that could possibly be further optimized as:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONSSET "_OSV="FOR /F "TOKENS=1* DELIMS=[" %%A IN ('VER') DO (SET "_OSV=%%B"	CALL SET "_OSV=%%_OSV:* =%%" & CALL SET "_OSV=%%_OSV:~,3%%")IF %_OSV% LSS 5.1 (GOTO :EOF) ELSE ( IF %_OSV% LSS 6.4 (GOTO W%_OSV%) ELSE (GOTO :EOF)):W6.3Win_81.cmdGOTO :EOF:W6.2Win_8_2K12.cmdGOTO :EOF:W6.1Win_7_2K8R2.cmdGOTO :EOF:W6.0Win_Vista_2K8.cmdGOTO :EOF:W5.1Win_XP_2K3.cmd

Cheers and Regards

Link to comment
Share on other sites

And I think that could possibly be further optimized as:

 

Well, if you want to play that game, it could possibly be further optimized as:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=2,3,4 delims=[.]" %%A IN ('ver') DO CALL :Maj_min %%A %%B %%CGOTO W%Maj_min%:Maj_minSET Maj_min=%2.%3ECHO %Maj_min%GOTO :EOF:W6.3ECHO Win_81.cmdGOTO :EOF:W6.2ECHO Win_8_2K12.cmdGOTO :EOF:W6.1ECHO Win_7_2K8R2.cmdGOTO :EOF:W6.0ECHO Win_Vista_2K8.cmdGOTO :EOF:W5.2:W5.1ECHO Win_XP_2K3.cmdGOTO :EOF:W5.00:w4.00ECHO Earlier NT OS

jaclaz

Link to comment
Share on other sites

And I think that could possibly be further optimized as:

Well, if you want to play that game, it could possibly be further optimized as:
What about:
@Echo off & Setlocal EnableExtensionsFor /f "tokens=1-2 delims=." %%A In ('Ver') Do Set _=%%A.%%BIf %_:~-2,1% Equ . (If %_:~-3% GEq 5.1 GoTo :W%_:~-3%)Echo= Unknown or Unsupported Operating System detectedPing -n 6 127.0.0.1 >Nul & GoTo :EOF:W6.3RunAs /User:Administrator Win_81.cmdGoTo :EOF:W6.2RunAs /User:Administrator Win_8_2K12.cmdGoTo :EOF:W6.1RunAs /User:Administrator Win_7_2K8R2.cmdGoTo :EOF:W6.0RunAs /User:Administrator Win_Vista_2K8.cmdGoTo :EOF:W5.1Win_XP_2K3.cmd
Link to comment
Share on other sites

Sure. :thumbup

But the idea in my snippet was to remove any "IF".

And also any "SET" can be removed:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=2,3,4 delims=[.]" %%A IN ('ver') DO CALL :Maj_min %%A %%BGOTO :EOF:Maj_minGOTO W%2.%3GOTO :EOF

or:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=2 delims=[" %%A IN ('ver') DO CALL :Maj_min %%~nAGOTO :EOF:Maj_minGOTO W%2GOTO :EOF

and if we are allowed WMIC, there is also:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "skip=1" %%A IN ('wmic os get version') DO GOTO :W%%~nAGOTO :EOF

which removes also the "CALL"

 

jaclaz

Link to comment
Share on other sites

Sure. :thumbup

But the idea in my snippet was to remove any "IF".

And also any "SET" can be removed:

<snip />

or:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=2 delims=[" %%A IN ('ver') DO CALL :Maj_min %%~nAGOTO :EOF:Maj_minGOTO W%2GOTO :EOF
To be truthful I have no idea if switching If and Set for Call would optimize the code any more than nesting another For loop would

For /f "tokens=2 delims=[" %%A In ('Ver') Do (For /f "tokens=2" %%B In ("%%~nA"	) Do GoTo :W%%B)
The trouble however in these instances is that you'll need to still code sections for all versions outputs prior to 5.1 which may effectively negate any optimization.

and if we are allowed WMIC, there is also:

@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "skip=1" %%A IN ('wmic os get version') DO GOTO :W%%~nAGOTO :EOF
which removes also the "CALL"
You would of course need to already know that the OS firstly has WMIC installed and I'd guess that the time to initialize the WMIC executable would effectively rule it out as optimization.

Also remember that the rare but possible Whistler Server releases have completely different version numbering systems:

Whistler Server Preview		Version 2250Whistler Server alpha		Version 2257Whistler Server interim release	Version 2267Whistler Server interim release	Version 2410
Link to comment
Share on other sites

The WMIC will surely slow down much :ph34r: the operation :yes:, but the script does look "simpler", and since all in all the "whole" original batch is "interactive" the overall time to execute will be much more dependent on the user response to the SET /P's.

 

I interpreted the "optimize" more as "making it simpler" than as "making it faster".

 

Versions "prior" that will execute a .CMD (not a .BAT) will mean either WIndows NT 4.00 or WIndows 2000 aka 5.00, adding a couple of labels as I did in post #18 won't do much harm, and IMHO if someone is running Windows Whistler Server, he/she has much more serious problems than these ;).

 

I remember having noticed some time ago that IF's were actually slowing down execution, as well as CALLs, cannot really say how they compare with SETs and FORs loops.

 

I will try putting together another small batch to time the execution of the various versions posted, possibly in a x100 or x1000 loop it will be possible to see which one is actually faster, though set apart the slower WMIC version I doubt that there can be much differences among the others. :unsure:

 

jaclaz

Link to comment
Share on other sites

I'll look forward to your test results!

Perhaps we should have a look too at just identifying anything less than 6.0.

I say that because 5.1 is the only system which needs treating differently by ghosttracer. 5.1 doesn't need the hibernation, (6.0 and above needs to be run as administrator), 5.1 needs a Set /p construct whereas choice.exe may be preferable from 6.0.

They are then running a detection batch to load one of two scripts, pre_vista.cmd and post_vista.cmd.

Link to comment
Share on other sites

Did a few tests, the CALL is slower than the second FOR loop :thumbup, and variable expansion wins over the IF's, but not by much.
I made a batch taking the time for the execution of the various versions 100 times repeating it for 10 times then took the average of each set of 100 executions.
Excluded the (as expected much slower) WMIC snippet with an average of 9,21 seconds :w00t:, the various versions I posted were all in a range of 1,59-1,62, whilst the snippet in post #19 averaged 1,54 and the one in post #21 1,53.
So, we are talking anyway of hundredths of seconds difference over one hundred executions.
Attached is the batch I used.

jaclaz

 

P.S.: The one in post #16 averaged 1,65 and the one in post #17 by bphlpt 1,64

run10times.zip

Edited by jaclaz
Link to comment
Share on other sites

  • 2 weeks later...

Good day

 

Sorry about the long reply. I was busy debugging etc.. Here is my final (hopefully) version of the code.

@ECHO OFF:: Ensure ADMIN Privileges - from http://www.robvanderwoude.com/clevertricks.php:: adaptation of https://sites.google.com/site/eneerge/home/BatchGotAdmin and http://stackoverflow.com/q/4054937:: Check for ADMIN Privileges >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"IF '%errorlevel%' NEQ '0' ( REM Get ADMIN Privileges    ECHO= Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"    ECHO= UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"    "%temp%\getadmin.vbs"    DEL "%temp%\getadmin.vbs"    EXIT /B) ELSE ( REM Got ADMIN Privileges    PUSHD "%CD%"    CD /D "%~dp0"):: END - Ensure ADMIN Privileges:: PrelimsSETLOCAL ENABLEDELAYEDEXPANSION  ENABLEEXTENSIONSSET "_OSV=" & FOR /f "TOKENS=2 DELIMS=[]" %%A IN ('VER') DO FOR /f "TOKENS=2,3 DELIMS=. " %%B IN ("%%~A") DO SET "_OSV=%%B.%%C"IF %_OSV% LSS 5.1 ECHO= This requires XP or greater & EXIT /bSET "_Arch=x64" & IF %PROCESSOR_ARCHITECTURE%==x86 IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET "_Arch=x86"ECHO PLease specify if the following programs needs to be installed. y for yes and n for noSET "AVG=N" & SET /p AVG=Do you want to Install AVG(Y/N)):: Begin work:: Installsecho Installing Firefox & ffox29.exe -msecho Installing Ashampoo & ashampoo.exe /sp- /verysilent /norestartecho Installing C-Cleaner & ccsetup.exe /Secho Installing K-lite & klite.exe /verysilent /norestartEcho Adding Reg Key for No Autorun & NOAUTORUN.regecho Killing Task & taskkill /f /im "burningstudio.exe"Echo Installing Adobe Reader & adobe11.exe  /sPB /sAll /msi /norestart ALLUSERS=1 EULA_ACCEPT=YESIF %_OSV% LSS 6.0 (    ECHO= Installing Internet Explorer 8 & IE8-WindowsXP-x86-ENU.exe /passive /update-no /no-default /norestart	Ren "C:\Documents and Settings\All Users\Desktop\CCleaner.lnk" "Run Weekly.lnk"	Ren "C:\Documents and Settings\All Users\Desktop\Mozilla Firefox.lnk" "Internet.lnk"	Del "C:\Documents and Settings\All Users\Desktop\Your Software Deals.lnk") ELSE (    ECHO= Internet Explorer 8 Not Required & ECHO= Disabling Hibernation & POWERCFG -h off)IF /i "%AVG:~,1%." == "Y." (    echo Installing AVG%_Arch% & avg%_Arch% /UILevel=silent /InstallToolbar=0 /ChangeBrowserSearchProvider=0 /SelectedLanguage=1033 /InstallSidebar=0 /ParticipateProductImprovement=0 /DontRestart /KillProcessesIfNeeded) Else (    echo AVG Installation Skipped):: TweaksEcho Renaming Icons...Ren "c:\Users\Public\Desktop\CCleaner.lnk" "Run Weekly.lnk"Ren "c:\Users\Public\Desktop\Mozilla Firefox.lnk" "Internet.lnk"Del "c:\Users\Public\Desktop\Your Software Deals.lnk" Echo Renaming Doneecho Disabling System RestoreReg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" /v DisableSR /t REG_DWORD /d 1 /fsc config srservice start= disablednet stop srservice:: CleanupFOR %%A IN (    ffox29.exe    ccsetup.exe    adobe11.exe    klite.exe    IE8-WindowsXP-x86-ENU.exe    NOAUTORUN.reg    InstallV1.2.cmd    ashampoo.exe    avgx64.exe    avgx86.exe  ) DO (    ECHO Deleting file %%A ..... & DEL %%A)SET /p CONTINUE=Installs Complete.... Please install Libre Office and Adobe Update manually.exit /b

Thank you for everybody's help and suggestions. Do you see room for improvement? except for those rename commands when installing ie?

 

You will see that i removed LibreOffice installation due to it crashing the batch file. and the NoAutorun.reg needs me to click Yes and OK.

Edited by ghosttracer
Link to comment
Share on other sites

At first sight it's fine. :thumbup

 

Still at the scope of simplifying, you don't really-really need an IF/ELSE here:

IF /i "%AVG:~,1%." == "Y." (echo Installing AVG%_Arch% & avg%_Arch% /UILevel=silent /InstallToolbar=0 /ChangeBrowserSearchProvider=0 /SelectedLanguage=1033 /InstallSidebar=0 /ParticipateProductImprovement=0 /DontRestart /KillProcessesIfNeeded) Else (echo AVG Installation Skipped):: Tweaks

Personally I would use the simpler:

IF /i "%AVG:~,1%." == "N." echo AVG Installation Skipped&GOTO :Tweaksecho Installing AVG%_Arch% & avg%_Arch% /UILevel=silent /InstallToolbar=0 /ChangeBrowserSearchProvider=0 /SelectedLanguage=1033 /InstallSidebar=0 /ParticipateProductImprovement=0 /DontRestart /KillProcessesIfNeeded:Tweaks

but it is only a matter of tastes/preferences, to actually skip code while printing to console that you are skipping some code ;).

 

jaclaz

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...