Search code examples
batch-fileunicoderegistry

Check if the registry has numeric value


I have a registry key of type REG_SZ. I want to identify if the registry has numeric values

for /f "tokens=1,2,3 delims== " %%x in ('regdmp HKLM\Software\MyRegistry ^|  Find "v1"') do set myValue=%%y
SET "var1="&for /f "delims=0123456789" %%c in ("%myValue%") do set var1=%%c
if defined var1 echo "Not Numeric" else "Numeric"

This code works if the V1 has English characters and numbers. It doesn't work if V1 has Unicode character.


Solution

  • I'd suggest some small changes.

    1. Use reg instead of regdmp to get the v1 directly
    2. Use set to try a calculation against the variable to check if it's numeric
    3. Use skip=2 in the for loop to jump past the header lines

    Here's a version with these adjustments.

    @echo off
    setlocal
    for /f "tokens=1,2,3 skip=2" %%x in ('reg query HKLM\Software\MyRegistry /v v1') do set myValue=%%z
    set /a var1=myValue*1
    if x%var1% equ x%myValue% (
        echo "Numeric"
    ) else (
        echo "Not Numeric" 
    )