Computername variable in cmd Computername variable in cmd windows windows

Computername variable in cmd


This works here:

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


You can do this with substring commands, as per the following transcript:

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

That final (chained) if statement is the one you're looking for to see if your variable starts with KM and ends with 00.

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


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

You can try also with hostname instead of echo %computername%