Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computername variable in cmd

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

I need a variable that takes a part of the computername.

I need a if statement that checks if the computername contains "KM" at the start and 00 at the end. It should not look at the number between KM and -00

KM100-00
KM200-00
like image 927
Stefan Frederiksen Avatar asked Sep 10 '13 07:09

Stefan Frederiksen


People also ask

How do you declare a variable in CMD?

To reference a variable in Windows, use %varname% (with prefix and suffix of '%' ). For example, you can use the echo command to print the value of a variable in the form " echo %varname% ".

Can you set variables in CMD?

Syntax SET variable SET variable=string SET "variable=string" SET "variable=" SET /A "variable=expression" SET /P variable=[promptString] SET " Key variable : A new or existing environment variable name e.g. _num string : A text string to assign to the variable.

How do I display a variable in CMD?

Select Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable. For example, to check if NUKE_DISK_CACHE is set, enter echo %NUKE_DISK_CACHE%.

How do I find my hostname in CMD?

Locating Your Computer's Hostname on a PC (Windows 10)In the window the window that appears on the bottom-left hand corner of your screen, type in cmd and click OK. The command prompt window will appear. In this window, type hostname and press Enter. The name of your computer will be displayed.

What is hostname in CMD?

Displays the host name portion of the full computer name of the computer. Important. This command is available only if the Internet Protocol (TCP/IP) protocol is installed as a component in the properties of a network adapter in Network.


4 Answers

This works here:

echo %computername%| findstr "^KM.*00$" >nul && echo found the right format
like image 89
foxidrive Avatar answered Oct 23 '22 18:10

foxidrive


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

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

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.

like image 26
paxdiablo Avatar answered Oct 23 '22 20:10

paxdiablo


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%

like image 2
npocmaka Avatar answered Oct 23 '22 20:10

npocmaka


I recommend you to read this page, which is about substring usage in command prompt.

And why dont you try this;

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
like image 1
Sarge Avatar answered Oct 23 '22 19:10

Sarge