Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch script how to check a variable if all letters and/or numbers  (Read 23721 times)

0 Members and 1 Guest are viewing this topic.

april

    Topic Starter


    Rookie

    I need to check a variable in my dos batch script if it contains all letter and/or all numbers.

    If it's all letters, then perform <actions x>
    If it's all numbers, then perform <actions y>
    If it's letters and numbers, then perform <actions z>
    Otherwise, error

    Your help is greatly appreciated.
    Thanks.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Batch script how to check a variable if all letters and/or numbers
    « Reply #1 on: January 19, 2009, 08:27:50 AM »
    Nothing is idiot proof in batch code and regular expressions are no exception. This little snippet demonstrates method.

    Code: [Select]
    @echo off
    setlocal
    set /p var=Enter var:
    echo %var%|findstr /r "[^0-9]" > nul
    if errorlevel 1 actionX & goto tag
    echo %var%|findstr /r "[^a-zA-Z]" > nul
    if errorlevel 1 actionY & goto tag
    echo %var%|findstr /r "[^0-9a-zA-Z]" > nul
    if errorlevel 1 (actionZ) else echo error
    :tag

    The snippet requests a string entered at the console. You should be able to modify the logic and incorporate into your own script.

    Good luck.  8)

    If any of the NT batch special characters exist in the string, the code will break.
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    april

      Topic Starter


      Rookie

      Re: Batch script how to check a variable if all letters and/or numbers
      « Reply #2 on: January 19, 2009, 08:47:33 AM »
      Great!  That worked.
      Thanks.