A
A
Andrey Strelkov2020-01-29 08:24:05
cmd/bat
Andrey Strelkov, 2020-01-29 08:24:05

How to use a .bat file to perform a certain action depending on the name of the computer on which the file is being executed?

Good afternoon, please tell me if it is possible to check a part of the computer name using the .bat file and, based on this, perform this or that action
Namely, there are 5 computers with the following names:

  • 001-comp_a
  • 002-comp_b
  • 002-comp_c
  • 003-comp_d
  • 004_comd_e


It is necessary to make it so that when running the .bat file on all computers where the name starts with 001, let's say the message 001 is displayed, and where the computer name starts at 002, respectively, 002 (and so with other cases similarly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2020-01-29
@strelkov_av

Approximately like this:

@echo off

rem Имя компьютера из переменной окружения COMPUTERNAME
set fullName=%COMPUTERNAME%
rem echo %fullName%

rem Первые три симовла имени
set subName=%fullName:~0,3%
rem echo %subName%

rem Если имя начинается на 001 вывести true иначе false
IF %subName%==001 (echo true) ELSE (echo false)

rem Если имя начинается на 002 вывести true иначе false
IF %subName%==002 (echo true) ELSE (echo false)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question