DEV Community

Tan Yu Xuan
Tan Yu Xuan

Posted on

2 1

I found it. The "most-complex" way to check operating system build number before running an batch script.

So, technically, days ago, when I was doing stuffs for my own project, which is WSATools, and then I think I made the most complex way to check operating system's build number before running stuffs.

Why did you do this?

So, Windows Subsystem for Android requires Windows 11, and it requires Windows 11. I also asked Stack Overflow, but technically no solution, which the only ones are just checking the OS version through Command Prompt, but didn't continue running.

The actual command that I used to run it. Modified a little bit so I can explain it

Before you try running

Make sure you downloaded any version of fart.exe. I built a version and included it on WSATools, and you can download v1.99c of fart for Windows here.

ver >> checkver.bat
fart checkver.bat "Microsoft Windows [Version" " " >nul
fart checkver.bat "]" " " >nul
fart -C checkver.bat \r\n " " >nul
fart -C checkver.bat "   " "@echo off \r\n echo " >nul
fart checkver.bat "  " " " >nul
fart -C checkver.bat ".282 " ".282\r\n" >nul
fart checkver.bat " echo " "echo " >nul
for /f %%N in ('checkver') do set "check=%%N"
if %check% == 10.0.22000.282 (
del checkver.bat
echo Success
  pause >nul
) else (
del checkver.bat
echo Failed
)
Enter fullscreen mode Exit fullscreen mode

And now, here's the explanation

ver >> checkver.bat: This is used for getting the ver command output to a batch file.
Which, of course, Opening checkver.bat on any editing software will give us this, which can't be runned directly:


Microsoft Windows [Version 10.0.22000.282]
Enter fullscreen mode Exit fullscreen mode

And in the next step, we will make it runnable by making it echo the OS build number through command prompt and check with the terms at the original file. The output of the file will be something like this:

G:\git-working\wsatools>checkver
10.0.22000.282
Enter fullscreen mode Exit fullscreen mode

The fart checkver.bat commands: a breakdown

Here's what the commands will run, and the output itself:

fart checkver.bat "Microsoft Windows [Version" " "

  • Replace \nMicrosoft Windows [Version 10.0.22000.282] to \n10.0.22000.282]

fart checkver.bat "]" " "

  • Replace \n10.0.22000.282] to \n10.0.22000.282

fart -C checkver.bat \r\n " "

  • Remove new line.

fart -C checkver.bat " " "@echo off \r\n echo "

  • Transform it into a batch file by adding echo command that we will use later. Replaced to @echo off\n echo 10.0.22000.282.

fart checkver.bat " " " "

  • Remove some spaces.

fart -C checkver.bat ".282 " ".282\r\n"

  • This also removes some spaces. You can replace .282 to the last numbers of the build number. For example:
    • Windows 10 21H1 (10.0.19043.1202) should replace .282 to .1202

fart checkver.bat " echo " "echo "

  • Also removes spacing before echo.

Check output for checkver.bat. It should return just the version.

for /f %%N in ('checkver') do set "check=%%N"
if %check% == 10.0.22000.282 (
del checkver.bat
echo Success
  pause >nul
) else (
del checkver.bat
echo Failed
)
Enter fullscreen mode Exit fullscreen mode

For del checkver.bat, this deletes the checkver.bat batch file for future uses.

And finally, compare the output in checkver.bat with the build number set. Change it to if %check == (build number).

Change echo Success to point to the file which you want to run, or just directly put the batch script right there.

Or else, for echo Failed, you can put a failed message there.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay