[Batch] OS identification select output

Discussion in 'Scripting' started by Humphrey, Nov 29, 2013.

  1. Humphrey

    Humphrey MDL Expert

    Dec 13, 2011
    1,458
    982
    60
    #1 Humphrey, Nov 29, 2013
    Last edited by a moderator: Apr 20, 2017
    Wondering if there is a bit simpler way to write the code below.
    I am trying to find a faster response in this code. Seems it takes a little to long.

    Code:
    @ECHO OFF
    REM Setup
    SetLocal EnableDelayedExpansion
    :: Version detection- send user to appropriate Windows CMD Decoy screen. 
    FOR /f "tokens=1,2,3* delims=." %%a IN ('ver') DO (
    SET WVer=%%a
    SET WVer=!WVer:~-1!
    SET WVer=!WVer!.%%b.%%c
    SET WVer=!WVer:]=!
    )
    ver | findstr /i "6\.3\." > nul
    If !errorlevel! equ 0 VER & ECHO.(c) 2013 Microsoft Corporation. All rights reserved. & GOTO Win8CMD
    ver | findstr /i "6\.2\." > nul
    If !errorlevel! equ 0 VER &  ECHO.(c) 2012 Microsoft Corporation. All rights reserved. & GOTO Win8CMD
    ver | findstr /i "6\.1\." > nul
    If !errorlevel! equ 0 VER & ECHO.Copyright (c) 2009 Microsoft Corporation.  All rights reserved. & GOTO Win7CMD
    ver | findstr /i "6\.0\." > nul
    If !errorlevel! equ 0 VER & ECHO.Copyright (c) 2006 Microsoft Corporation.  All rights reserved. & GOTO WinVISTACMD
    ver | findstr /i "5\.1\." > nul
    If !errorlevel! equ 0 VER &  ECHO ECHO.(C) Copyright 1985-2001 Microsoft Corp. & GOTO WinXPCMD
    REM Decoy Screens
    :Win8CMD
    REM Windows 8.1 Command Prompt
    TITLE Command Prompt
    ECHO.
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Humphrey

    Humphrey MDL Expert

    Dec 13, 2011
    1,458
    982
    60
    #2 Humphrey, Nov 30, 2013
    Last edited by a moderator: Apr 20, 2017
    (OP)
    If anyone was wondering, this is an update to this thread: http://forums.mydigitallife.net/thr...iles/page7?p=740767&highlight=chat#post740767

    I made this a while back for when I worked at a call center so that the employes could chat over shard networked drives.


    (Not final release)
    Code:
    
    :: Batch Messenger Chat Rev 3.0 (11/29/2013)
    :: Created By: Humphrey
    
    @ECHO OFF
    :: Setup
    SetLocal EnableDelayedExpansion
    :: First time handle
    SET 1sthandle=!Username!
    :: Setup prefered time/date.
    FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET NowT=%%A & FOR /F "TOKENS=*" %%A IN ('DATE/T') DO SET NowD=%%A
    :: Version detection- send user to appropriate Windows CMD Decoy screen. XP through 8.1
    FOR /f "tokens=1,2,3* delims=." %%a IN ('ver') DO (SET WVer=%%a & SET WVer=!WVer:~-1! & SET WVer=!WVer!.%%b.%%c & SET WVer=!WVer:]=!)
    ver | findstr /i "6\.3\." > nul
    If !errorlevel! equ 0 VER & ECHO.(c) 2013 Microsoft Corporation. All rights reserved. & GOTO :Win8CMD
    ver | findstr /i "6\.2\." > nul
    If !errorlevel! equ 0 VER &  ECHO.(c) 2012 Microsoft Corporation. All rights reserved. & GOTO :Win8CMD
    ver | findstr /i "6\.1\." > nul
    If !errorlevel! equ 0 VER & ECHO.Copyright (c) 2009 Microsoft Corporation.  All rights reserved. & GOTO :Win7CMD
    ver | findstr /i "6\.0\." > nul
    If !errorlevel! equ 0 VER & ECHO.Copyright (c) 2006 Microsoft Corporation.  All rights reserved. & GOTO :WinVISTACMD
    ver | findstr /i "5\.1\." > nul
    If !errorlevel! equ 0 VER &  ECHO ECHO.(C) Copyright 1985-2001 Microsoft Corp. & GOTO :WinXPCMD
    :: Decoy Screens
    :Win8CMD
    :: Windows 8.1 Command Prompt
    TITLE Command Prompt
    GOTO :login
    :Win7CMD
    :: Windows 7 Command Prompt
    TITLE C:\Windows\system32\cmd.exe
    GOTO :login
    :WinVISTACMD
    :: Windows Vista Command Prompt
    TITLE C:\Windows\system32\cmd.exe
    GOTO :login
    :WinXPCMD
    :: Windows XP Professional Command Prompt
    TITLE C:\WINDOWS\system32\cmd.exe
    GOTO :login
    :login
    :: Query Login 
    SET Login=
    ECHO.
    SET /P Login=!Userprofile!^>
    IF /i "!Login!"=="letmein" CLS & GOTO :1stname
    ) ELSE (
    ECHO.'!Login!' is not recognized as an internal or external command,
    ECHO.operable program or batch file.
    ECHO.
    GOTO :login
    
    :1stname
    reg query HKCU\Environment /v handle 2>NUL
    IF !ErrorLevel! EQU 0 GOTO :MainMenu
    ECHO. Name currently set as [!1sthandle!].
    ECHO.
    choice /C YN /M "Do you wish to keep this as your name?:"
    if !ERRORLEVEL! EQU 1 SETX handle !1sthandle! & GOTO :MainMenu
    if !ERRORLEVEL! EQU 2 ECHO. & Set /p Handle= ^<Enter your new name:^>:
    SETX Handle !Handle!
    :MainMenu
    TITLE Main Menu
    CLS
    ECHO.
    ECHO. ^<Your name is: !handle!^>
    ECHO.
    ECHO. ^</Main Manu/^>
    ECHO.
    ECHO. ^[A^] Information
    ECHO.
    ECHO. ^[B^] Chat Rooms
    ECHO.
    ECHO. ^[C^] Change Name
    ECHO.
    ECHO. ^[D^] Exit
    ECHO.
    choice /C ABCDE /N /M "Select menu item:"
    if !ERRORLEVEL! EQU 5 reg delete HKCU\Environment /v handle /F & PAUSE >NUL & EXIT
    if !ERRORLEVEL! EQU 4 EXIT >NUL
    if !ERRORLEVEL! EQU 3 ECHO. & SET /P handle=^</Enter your new name here/^>: & SETX handle !handle! & CLS & GOTO :MainMenu
    if !ERRORLEVEL! EQU 2 CLS & goto :Rooms
    if !ERRORLEVEL! EQU 1 CLS & goto :Information
    :Information
    ECHO.
    ECHO. ^</Basic Info/^>
    ECHO.
    ECHO. This chat script is limited to specific 
    ECHO. conditions in which it can function.
    ECHO.
    ECHO. It can only function in the way it is ment to
    ECHO. if it can acess a shared directory. Ex: Two
    ECHO. computers on a network can access directory Z:\Chat\Chat.log
    ECHO. That way, the two computers located on said network
    ECHO. can run this chat script and it be able to access the
    ECHO. previously mentioned Z:\Chat\Chat.log so that it can read
    ECHO. and write. That file- is basically the chat room. 
    ECHO.
    ECHO. This chat script will also function over DropBox. Place the
    ECHO. script inside a shared folder and start chatting. Just be careful
    ECHO. not to talk to quickly cause DropBox has limits on modificiations
    ECHO. of files because it creates upload conflicts. 
    ECHO.
    PAUSE & GOTO :MainMenu
    :Rooms
    ECHO.
    ECHO. ^<While in a chat room, type /Help for information^>
    ECHO.
    ECHO. Under Construction 
    ECHO.
    PAUSE
    GOTO :ChatScript
    
    
    
    :ChatScript
    IF NOT EXIST Chat.log ECHO.>>Chat.Log
    ECHO.!NowT![!HANDLE!] joined the discussion.>>Chat.log & ECHO.>>Chat.log
    :MESSAGE
    IF NOT EXIST Chat.log ECHO.>>Chat.Log
    set message=
    CLS
    ECHO.
    TYPE Chat.log
    ECHO.
    ECHO.__________________________
    set /p message= !HANDLE!: 
    if not defined message goto message 
    IF /I "!message!"=="/del" del /Q chat.log & ECHO.!NowT![!HANDLE!]: deleted logs on !NowD!>>Chat.log & ECHO.>>Chat.log & CLS & GOTO :MESSAGE
    IF /I "!message!"=="/Main" ECHO.>>Chat.log & ECHO.!NowT![!HANDLE!]: exited the discussion.>>Chat.log & ECHO.>>Chat.log & GOTO :MainMenu
    IF /I "!message!"=="/nick" Set OldHandle=!HANDLE!& ECHO. & set /p handle=^</Enter your new name here/^>: & SETX Handle !HANDLE! & ECHO.!NowT![!OldHandle!]: changed name to [!Handle!]>>Chat.log & GOTO :Message
    IF /I "!message!"=="/Main" ECHO.>>Chat.log & ECHO.!NowT![!HANDLE!]: exited the discussion.>>Chat.log & ECHO.>>Chat.log & EXIT
    IF /I "!message!"=="/Help" CLS & GOTO :ChatCommands
    >> Chat.log echo !NowT![!handle!]: !message!
    )
    goto message
    
    :ChatCommands
    ECHO. & ECHO. ^</These are a list of commands to use while in a chat room. /^>
    ECHO. & ECHO. & ECHO. /Help = Displays this help information.
    ECHO. & ECHO. /Main = Return to the main menu.
    ECHO. & ECHO. /Nick = Changes your name. (Handle- A enviromental setting.)
    ECHO. & ECHO. /Del  = Deletes your current chat rooms chat logs. (There are no backups.)
    ECHO. & ECHO. /Exit = To leave the chat and close the command prompt.
    ECHO. & ECHO.
    PAUSE & GOTO MESSAGE
    
    
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. HALIKUS

    HALIKUS MDL Addicted

    Jul 29, 2009
    527
    369
    30
    #3 HALIKUS, Jan 29, 2014
    Last edited by a moderator: Apr 20, 2017
    Something like this maybe? You can substitute :BIOS for an edition. I usually use this in my setupcomplete.cmd to determine witch loader to use if necessary.

    Code:
    @echo off
    
    VER | findstr /l "6.3." > nul
    IF %ERRORLEVEL% EQU 0 goto :81
    
    VER | findstr /l "6.2." > nul
    IF %ERRORLEVEL% EQU 0 goto :8
    
    VER | findstr /l "6.1." > nul
    IF %ERRORLEVEL% EQU 0 goto :7
    
    VER | findstr /l "6.0." > nul
    IF %ERRORLEVEL% EQU 0 goto :Vista
    
    :81
    Echo Win8.1
    
    goto :end
    
    
    :8
    Echo Win8
    
    goto :end
    
    
    :7
    Echo Win7
    SET EDITION=&SET CSNAME=&SET CSVENDOR=&SET SYSMODEL=&SET SYSMNF=&SET BASEPRO=&SET BASEMNF=&SET BIOVER=&SET MAN=DEFAULT&SET MAN2=&SET PIDKEY=&SET REALMAN=&SET WINVER=&SET INFO=INFO&SET LAST=LAST&SET INL=IF NOT ERRORLEVEL 1&SET RESLN=&SET SLIC=SLIC
    SET OEMS=%windir%\Setup\scripts\oemscan.ini&SET RQR1=REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName"&SET RQR2=REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "EditionId"
    %RQR2% | findstr /I "\<ENTERPRISE\>" >nul&&%INL% SET EDITION=ENTERPRISE&goto :BIOS
    
    
    goto :end
    
    
    :BIOS
    
    goto :end
    
    
    :Vista
    
    goto :end
    
     
  4. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,530
    655
    60
    #4 Flipp3r, Jan 29, 2014
    Last edited by a moderator: Apr 20, 2017
    Just had a look at one of my old batch files I used. It may be quicker than using findstr:
    Code:
    ver | find "6.1" > nul
    if %ERRORLEVEL% == 0 goto ver_Win7
    
    ver | find "XP" > nul
    if %ERRORLEVEL% == 0 goto ver_xp
    
    
    ver | find "6.0" > nul
    if %ERRORLEVEL% == 0 goto ver_Vista
    
    ver | find "5.2" > nul
    if %ERRORLEVEL% == 0 goto ver_2003
    
    echo Machine undetermined.
    goto exit
     
  5. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    104
    10
    #5 Compo, Jan 30, 2014
    Last edited by a moderator: Apr 20, 2017
    The process is 'slow' because you are using multiple VER commands and piping the output of each one through separate FINDSTR or FIND commands.

    Therefore try running only one VER command and do not pipe its output!

    Here's an example script:
    Code:
    @ECHO OFF
    SETLOCAL
    (SET OSV=)
    FOR /F "TOKENS=1* DELIMS=[" %%A IN ('VER') DO (CALL :GMM %%B)
    
    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))))
    
    REM Windows 8.1 Commands below here
    ECHO(Windows 8.1
    PING -n 4 127.0.0.1 1>NUL
    GOTO ENDIT
    
    :W82K12
    REM Windows 8 or Server 2012 Commands below here
    ECHO(Windows 8 or Server 2012
    PING -n 4 127.0.0.1 1>NUL
    GOTO ENDIT
    
    :W72K8R2
    REM Windows 7 or Server 2008 R2 Commands below here
    ECHO(Windows 7 or Server 2008 R2
    PING -n 4 127.0.0.1 1>NUL
    GOTO ENDIT
    
    :VST2K8
    REM Windows Vista or Server 2008 Commands below here
    ECHO(Windows Vista or Server 2008
    PING -n 4 127.0.0.1 1>NUL
    GOTO ENDIT
    
    :XP2K3
    REM Windows XP or Server 2003 Commands below here
    ECHO(Windows XP or Server 2003
    PING -n 4 127.0.0.1 1>NUL
    
    :ENDIT
    GOTO :EOF
    
    :GMM
    (SET OSV=%2)
    (SET OSV=%OSV:~,3%)
    IF %OSV% GEQ 5.1 (IF %OSV% LEQ 6.3 GOTO :EOF)
    ECHO(UNSUPPORTED OPERATING SYSTEM
    PING -n 4 127.0.0.1 1>NUL
    EXIT
    This is designed to exclude Operating Systems earlier than Windows XP/Server 2003 and later than Windows 8.1.

    To re-use the code, all you need to do is replace each pair of lines I used, (starting with ECHO & PING), directly below the lines starting with REM with your Operating System specific code.

    Hope it helps you out!
     
  6. xinso

    xinso MDL Guru

    Mar 5, 2009
    4,502
    5,601
    150
    #6 xinso, May 30, 2014
    Last edited by a moderator: Apr 20, 2017
    Compo,

    Thank you.