Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: IP ADDRESSES from Computer Names - uses ping.exe to get a the information[Next]: LoggedOnLocallyUsers - useful over remote computers
\->Batch Files->IsIpAddress() - determine is a string looks valid as an IP4 address

IsIpAddress() [determine is a string looks valid as an IP4 address].cmd

Para 1 ($todo)

Para 2 ($todo)

[anchor]

The Code for: "IsIpAddress() [determine is a string looks valid as an IP4 address].cmd"

This is the example, a shortcut was installed to this code :

@echo off
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[cmd].IsIpAddress() [determine is a string looks valid as an IP4 address].cmd.pvcs   1.0   11 Jul 2014 19:31:02   USER "Dennis"  $
::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocal

echo TESTING: IsIpAddress()
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call :TEST "1.2.3.4"    "Y"
call :TEST "1.2.3.04"   "N"
call :TEST "01.2.3.4"   "N"
call :TEST "1"          "N"
call :TEST "1.2"        "N"
call :TEST "1.2.3"      "N"
call :TEST ""           "N"
call :TEST "1.2.3.0"    "Y"
call :TEST ".2.3.4"     "N"
call :TEST "1.2..4"     "N"
goto :EOF

echo.
echo NOTE: Current code won't pick this up as a mistake
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
goto :EOF


::@@@@@@@@@@@
:TEST
::@@@@@@@@@@@
    set  TestIpAddress=%~1
    set ExpectedAnswer=%~2
    echo TESTING IP ADDRESS: "%TestIpAddress%"
         call :IsIpAddress "IpAddressPassed" "%TestIpAddress%"
         ::if "%IpAddressPassed%" == "Y" goto IpWasPassed
    echo   * IS IP ADDRESS: %IpAddressPassed%
    if     "%IpAddressPassed%" == "%ExpectedAnswer%" echo   * CORRECT :-)
    if not "%IpAddressPassed%" == "%ExpectedAnswer%" echo   * ####### ERROR: Incorrect Result... #########
    echo.
    goto :EOF



::+++++++++++++++++++++++++++++++
:IsIpAddress
::+++++++++++++++++++++++++++++++ Version 14.150c
    ::--- Init ---------------------------------------------------------------
    set   ResultVar=%~1
    set        ThisIp=%~2
    set %ResultVar%=N



    ::--- Expect 4 octects (and no more) ------------------------------------
    set O1=& set O2=& set O3=& set O4=& set O5=
    for /F "tokens=1,2,3,4,5 delims=." %%i in ("%ThisIp%") do set O1=%%i& set O2=%%j& set O3=%%k& set O4=%%l& set O5=%%m
    if     "%O1%" == "" goto :EOF
    if     "%O2%" == "" goto :EOF
    if     "%O3%" == "" goto :EOF
    if     "%O4%" == "" goto :EOF
    if not "%O5%" == "" goto :EOF

    ::--- Don't Allow OCTAL IP addresses from TANDEM ------------------------
    if not "%O1%" == "0" if "%O1:~0,1%" == "0" goto :EOF
    if not "%O2%" == "0" if "%O2:~0,1%" == "0" goto :EOF
    if not "%O3%" == "0" if "%O3:~0,1%" == "0" goto :EOF
    if not "%O4%" == "0" if "%O4:~0,1%" == "0" goto :EOF

    ::--- Check only digits and full stop -----------------------------------
    set HashIfIp=%ThisIp:.=%#
    set HashIfIp=%HashIfIp:0=%
    set HashIfIp=%HashIfIp:1=%
    set HashIfIp=%HashIfIp:2=%
    set HashIfIp=%HashIfIp:3=%
    set HashIfIp=%HashIfIp:4=%
    set HashIfIp=%HashIfIp:5=%
    set HashIfIp=%HashIfIp:6=%
    set HashIfIp=%HashIfIp:7=%
    set HashIfIp=%HashIfIp:8=%
    set HashIfIp=%HashIfIp:9=%
    if "%HashIfIp%" == "#" set %ResultVar%=Y
    goto :EOF

Please note that that I am not trying to show how great I am by producing batch files 9,000 characters long on one line that no one will understand or be able to debug when they go wrong. I am going out of my way to comment the code and make it verbose so beginners and advanced users will both benefit. I don't claim to be an expert that knows everything, if I'm wrong or make a mistake then please contact me and let me know :-)


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: IP ADDRESSES from Computer Names - uses ping.exe to get a the information[Next]: LoggedOnLocallyUsers - useful over remote computers


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.