Script only by computer name...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Script only by computer name...

#1 Post by gilb » 15 Feb 2018 06:38

Hi,
How to run command in script only if computer name start with XXX ?
like XXX01 / XXX02 / XXX03
Thanks, Gil.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Script only by computer name...

#2 Post by miskox » 15 Feb 2018 08:04

@echo off
if "%computername:~0,3%"=="XXX" echo Run a command
There you can insert a command.

Saso

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Re: Script only by computer name...

#3 Post by gilb » 15 Feb 2018 10:18

What is ~0,3% for?
what ~ for?
0,3 for?

also do I need to write at the end of line "Run a command"???

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Script only by computer name...

#4 Post by aGerman » 15 Feb 2018 12:03

gilb wrote:
15 Feb 2018 10:18
What is ~0,3% for?
what ~ for?
0,3 for?
Execute SET /? or have a look at the command index.
gilb wrote:
15 Feb 2018 10:18
also do I need to write at the end of line "Run a command"???
If you want to execute a command if the comparison matches then you certainly should write this command instead.

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Script only by computer name...

#5 Post by miskox » 16 Feb 2018 03:28

If you would give us information what you want we could give you a complete solution: for example: "I want to start abc.exe if computer name starts with XXX..."

Saso

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Re: Script only by computer name...

#6 Post by gilb » 17 Feb 2018 09:04

Did this:

---------
IF /i %computername:~0,6%%== mokedm

(Do...)

ELSE IF /i %computername:~0,5%%== moked

(Do...)
-------------

Not working
what's wrong here?

Thanks, GIl

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script only by computer name...

#7 Post by Squashman » 17 Feb 2018 11:07

gilb wrote:
17 Feb 2018 09:04
Did this:

---------
IF /i %computername:~0,6%%== mokedm

(Do...)

ELSE IF /i %computername:~0,5%%==moked

(Do...)
-------------

Not working
what's wrong here?

Thanks, GIl
I see you didn't even come close to reading the help file for the IF command.

Code: Select all

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF /i %computername:~0,6%==mokedm  (
        echo yes
    ) ELSE (
        echo no
    )

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Re: Script only by computer name...

#8 Post by gilb » 18 Feb 2018 03:13

howto?
IF /i %computername:~0,6%==mokedm (
echo yes
) ELSE (
echo no
)

how to do it with.. ELSE IF?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Script only by computer name...

#9 Post by aGerman » 18 Feb 2018 05:51

I don't understand your question. Just use ELSE IF :?

Code: Select all

if /i "%computername:~0,6%"=="mokedm" (
  echo yes
) else if /i "%computername:~0,5%"=="abcde" (
 echo yes
) else (
  echo no
)
Steffen

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Re: Script only by computer name...

#10 Post by gilb » 20 Feb 2018 10:24

I wrote this:

Code: Select all

IF /i 

"%computername:~0,5%"=="MOKED"(

but pc name is MOKED01

this is not runs the command in the if statemant

howto fix it?
Thanks
Last edited by Squashman on 20 Feb 2018 10:27, edited 1 time in total.
Reason: MOD EDIT: PLEASE USE CODE TAGS.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script only by computer name...

#11 Post by Squashman » 20 Feb 2018 10:26

gilb wrote:
20 Feb 2018 10:24
I wrote this:
IF /i

"%computername:~0,5%"=="MOKED"(


but pc name is MOKED01

this is not runs the command in the if statemant

howto fix it?
Thanks
Going forward please use CODE tags in your responses.

Has any example we have given you shown you that you can put the IF command on its own line and the comparison on a separate line?

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Re: Script only by computer name...

#12 Post by gilb » 21 Feb 2018 01:55

How to do it that will work?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Script only by computer name...

#13 Post by Squashman » 21 Feb 2018 09:43

gilb wrote:
21 Feb 2018 01:55
How to do it that will work?
Steffen gave you the code in his post

gilb
Posts: 11
Joined: 15 Feb 2018 06:08

Batch File script - by computer name

#14 Post by gilb » 07 Mar 2018 02:10

Hi,
Did this:

---------
@echo off


IF /I %COMPUTERNAME:~0,3%==PCM

(Do THIS1)

ELSE IF /i %computername:~0,3%== "PCS

(DO THIS2)

ELSE IF /i %computername:~0,2%%= "PC"

(DO THIS3)

--------------


got 3 pcs:

1 = PCM01
2= PCS01
3 = PC01

when run script on 1 - it need to do = "DO THIS1"
when run script on 2 - it need to do = "DO THIS2"
when run script on 3 - it need to do = "DO THIS3"

BUT NOTHING Happends
What I'm doing wrong?

Thanks, Gil

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch File script - by computer name

#15 Post by ShadowThief » 07 Mar 2018 04:19

Well, if it looks exactly like that, it's because your parentheses are in the wrong place. You also need parentheses on both sides of the == operator, and there were a few other typos that were breaking things.

Code: Select all

@echo off

IF /I "%COMPUTERNAME:~0,3%"=="PCM" (
	Do THIS1
) ELSE IF /i "%computername:~0,3%"=="PCS" (
	DO THIS2
) ELSE IF /i "%computername:~0,2%"=="PC"(
	DO THIS3
)

Locked