First four charancters of computer name

Discussion in 'Scripting' started by hypedave, Jan 25, 2017.

  1. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #1 hypedave, Jan 25, 2017
    Last edited by a moderator: Apr 20, 2017
    I'm trying output a echo based on first four characters of multiple computer names. Here is what I have so far but it's not working.

    Code:
    @echo off
    SET Name=%computername%
    SET FirstFourChars=%computername:~0,4%
    FOR %%A IN (aaaa bbbb cccc dddd eeee) DO IF %%A==%FirstFourChars% echo Test 1
    FOR %%A IN (ffff gggg hhhh jjjj kkkk) DO IF %%A==%FirstFourChars% echo Test 2
    exit
    
     
  2. bear_aussie

    bear_aussie MDL Junior Member

    Jun 8, 2015
    58
    29
    0
    #2 bear_aussie, Jan 28, 2017
    Last edited by a moderator: Apr 20, 2017
    not really enough info to diagnose - so ill take some guesses

    from the top, any reason for the unused Name envvar?

    second %COMPUTERNAME% tends to be all uppercase - if youre comparing it against lowercase strings that might be your issue

    try enabling command extensions at the beginning of your batch

    Code:
    SETLOCAL ENABLEEXTENSIONS
    and doing case insensitive compare in your IF statements

    Code:
    ... IF /I %%A EQU %FirstFourChars% echo Test 1
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...