Home > front end >  Need someone to help me check this batch file for errors
Need someone to help me check this batch file for errors

Time:01-28

I'm writing a batch script to detect the current system type, but I found that the result after I run it is not what I expected. And my computer's system is Windows 11.

Here is the source code:

@echo off
(ver|find "3.51." >nul && call :msg_yes "NT 3.5") || (call :msg_no "NT 3.5")
(ver|find "4.0." >nul && call :msg_yes 95) || (call :msg_no 95)
(ver|find "4.10." >nul && call :msg_yes 98) || (call :msg_no 98)
(ver|find "4.90." >nul && call :msg_yes ME) || (call :msg_no ME)
(ver|find "5.0." >nul && call :msg_yes 2000) || (call :msg_no 2000)
(ver|find "5.1." >nul && call :msg_yes XP) || (call :msg_no XP)
(ver|find "5.2." >nul && call :msg_yes 2003) || (call :msg_no 2003)
(ver|find "6.0." >nul && call :msg_yes Vista) || (call :msg_no Vista)
(ver|find "6.1." >nul && call :msg_yes 7) || (call :msg_no 7)
(ver|find "6.2." >nul && call :msg_yes 8) || (call :msg_no 8)
(ver|find "6.3." >nul && call :msg_yes 8.1) || (call :msg_no 8.1)
(ver|find "10.0.1" >nul && call :msg_yes 10) || (call :msg_no 10)
(ver|find "10.0.2" >nul && call :msg_yes 11) || (call :msg_no 11)

:msg_yes
echo Current System is Windows %~1

:msg_no
echo Current System is NOT Windows %~1

Result:

Current System is NOT Windows NT 3.5
Current System is NOT Windows 95
Current System is NOT Windows 98
Current System is NOT Windows ME
Current System is NOT Windows 2000
Current System is NOT Windows XP
Current System is NOT Windows 2003
Current System is NOT Windows Vista
Current System is NOT Windows 7
Current System is NOT Windows 8
Current System is NOT Windows 8.1
Current System is NOT Windows 10
Current System is Windows 11
Current System is NOT Windows 11
Current System is Windows
Current System is NOT Windows

I would like to know why it came out like Current System is NOT Windows 11, Current System is Windows and Current System is NOT Windows

Expected result:

Current System is NOT Wiandows NT 3.5
Current System is NOT Wiandows 95
Current System is NOT Wiandows 98
Current System is NOT Wiandows ME
Current System is NOT Wiandows 2000
Current System is NOT Wiandows XP
Current System is NOT Wiandows 2003
Current System is NOT Wiandows Vista
Current System is NOT Wiandows 7
Current System is NOT Wiandows 8
Current System is NOT Wiandows 8.1
Current System is NOT Wiandows 10
Current System is Windows 11

I also ran this script on a Windows 10 virtual machine and the same thing happened.
Result:

Current System is NOT Windows NT 3.5
Current System is NOT Windows 95
Current System is NOT Windows 98
Current System is NOT Windows ME
Current System is NOT Windows 2000
Current System is NOT Windows XP
Current System is NOT Windows 2003
Current System is NOT Windows Vista
Current System is NOT Windows 7
Current System is NOT Windows 8
Current System is NOT Windows 8.1
Current System is Windows 10
Current System is NOT Windows 10
Current System is NOT Windows 11
Current System is Windows
Current System is NOT Windows

Although I tried another way of writing it without ||, it worked fine, but I still want to add || and make sure it works

Code:

@echo off
ver|find "3.51." >nul && call :msg "NT 3.5" && exit /b
ver|find "4.0." >nul && call :msg 95 && exit /b
ver|find "4.10." >nul && call :msg 98 && exit /b
ver|find "4.90." >nul && call :msg ME && exit /b
ver|find "5.0." >nul && call :msg 2000 && exit /b
ver|find "5.1." >nul && call :msg XP && exit /b
ver|find "5.2." >nul && call :msg 2003 && exit /b
ver|find "6.0." >nul && call :msg Vista && exit /b
ver|find "6.1." >nul && call :msg 7 && exit /b
ver|find "6.2." >nul && call :msg 8 && exit /b
ver|find "6.3." >nul && call :msg 8.1 && exit /b
ver|find "10.0.1" >nul && call :msg 10 && exit /b
ver|find "10.0.2" >nul && call :msg 11 && exit /b

:msg
echo Current System is Windows %~1
pause >nul

Result:

Current System is Windows 11

I am a beginner in batch script, and I have also looked for some related tutorials, but I still can't do anything about it.

CodePudding user response:

Problem is that there is no GOTO jumps and script executes all lines of the code. You should use GOTO:eof like this:

@echo off
(ver|find "3.51." >nul && call :msg_yes "NT 3.5") || (call :msg_no "NT 3.5")
(ver|find "4.0." >nul && call :msg_yes 95) || (call :msg_no 95)
(ver|find "4.10." >nul && call :msg_yes 98) || (call :msg_no 98)
(ver|find "4.90." >nul && call :msg_yes ME) || (call :msg_no ME)
(ver|find "5.0." >nul && call :msg_yes 2000) || (call :msg_no 2000)
(ver|find "5.1." >nul && call :msg_yes XP) || (call :msg_no XP)
(ver|find "5.2." >nul && call :msg_yes 2003) || (call :msg_no 2003)
(ver|find "6.0." >nul && call :msg_yes Vista) || (call :msg_no Vista)
(ver|find "6.1." >nul && call :msg_yes 7) || (call :msg_no 7)
(ver|find "6.2." >nul && call :msg_yes 8) || (call :msg_no 8)
(ver|find "6.3." >nul && call :msg_yes 8.1) || (call :msg_no 8.1)
(ver|find "10.0.1" >nul && call :msg_yes 10) || (call :msg_no 10)
(ver|find "10.0.2" >nul && call :msg_yes 11) || (call :msg_no 11)

GOTO:eof


:msg_yes
echo Current System is Windows %~1

GOTO:eof

:msg_no
echo Current System is NOT Windows %~1

  • Related