Page 1 of 2

Script only by computer name...

Posted: 15 Feb 2018 06:38
by gilb
Hi,
How to run command in script only if computer name start with XXX ?
like XXX01 / XXX02 / XXX03
Thanks, Gil.

Re: Script only by computer name...

Posted: 15 Feb 2018 08:04
by miskox
@echo off
if "%computername:~0,3%"=="XXX" echo Run a command
There you can insert a command.

Saso

Re: Script only by computer name...

Posted: 15 Feb 2018 10:18
by gilb
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

Re: Script only by computer name...

Posted: 15 Feb 2018 12:03
by aGerman
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

Re: Script only by computer name...

Posted: 16 Feb 2018 03:28
by miskox
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

Re: Script only by computer name...

Posted: 17 Feb 2018 09:04
by gilb
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

Re: Script only by computer name...

Posted: 17 Feb 2018 11:07
by Squashman
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
    )

Re: Script only by computer name...

Posted: 18 Feb 2018 03:13
by gilb
howto?
IF /i %computername:~0,6%==mokedm (
echo yes
) ELSE (
echo no
)

how to do it with.. ELSE IF?

Re: Script only by computer name...

Posted: 18 Feb 2018 05:51
by aGerman
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

Re: Script only by computer name...

Posted: 20 Feb 2018 10:24
by gilb
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

Re: Script only by computer name...

Posted: 20 Feb 2018 10:26
by Squashman
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?

Re: Script only by computer name...

Posted: 21 Feb 2018 01:55
by gilb
How to do it that will work?

Re: Script only by computer name...

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

Batch File script - by computer name

Posted: 07 Mar 2018 02:10
by gilb
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

Re: Batch File script - by computer name

Posted: 07 Mar 2018 04:19
by ShadowThief
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
)