Computername variable in cmd

In CMD, the following variable will give you the computer name:% COMPUTERNAME%

I need a variable that takes part of the username.

I need an if statement that checks if the computer_name contains β€œKM” at the beginning and 00 at the end. He should not look at the number between KM and -00

KM100-00 KM200-00 
+4
source share
4 answers

This works here:

 echo %computername%| findstr "^KM.*00$" >nul && echo found the right format 
+3
source

You can do this using substring commands, according to the following decoding:

 pax> set xyzzy=KM100-00 KM200-00 pax> echo %xyzzy% KM100-00 KM200-00 pax> echo %xyzzy:~0,2% KM pax> echo %xyzzy:~-2,2% 00 pax> if %xyzzy:~0,2%==KM if %xyzzy:~-2,2%==00 echo yes yes 

This final (chained) if is the one you are looking to see if your variable starts with KM and ends at 00 .

The expression %X:~Y,Z% will give you the characters Z , starting at position Y (with zero base) of the variable X You can specify a negative Y value to make it relative to the end of the line.

+2
source
 echo %computername%| findstr /I /b "KM" | findstr /i /e "00" && echo computer name is like KM-XX-00 

You can also try hostname instead of echo %computername%

+2
source

I recommend that you read this page regarding the use of substring on the command line.

And why aren’t you trying to do it,

 set str=KM2000-00 echo.%str% set pre=%str:~0,2% echo.%pre% set pst=%str:~-2% echo.%pst% IF %pre% == KM( IF %pst% == 00( echo.true ) ) pause 
+1
source

Source: https://habr.com/ru/post/1501424/


All Articles