mosquito87
mosquito87

Reputation: 4440

Check registry values in windows batch

I would like to check the existence of some windows registry entries and their values in a .bat file.

So far I managed to check the existence:

@echo off

set SMB2_REGKEY=HKLM\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters
set SMB2_REGVAL1=FileInfoCacheLifetime
set SMB2_REGVAL2=FileNotFoundCacheLifetime
set SMB2_REGVAL3=DirectoryCacheLifetime

REM Check for presence of key first.
reg query %SMB2_REGKEY% /v %SMB2_REGVAL1% 2>nul || (echo Error! & exit /b 1)
reg query %SMB2_REGKEY% /v %SMB2_REGVAL2% 2>nul || (echo Error! & exit /b 1)
reg query %SMB2_REGKEY% /v %SMB2_REGVAL3% 2>nul || (echo Error! & exit /b 1)

How can I now check that the values of the three values (FileInfoCacheLifetime, FileNotFoundCacheLifetime, DirectoryCacheLifetime) are set to zero?

Upvotes: 1

Views: 667

Answers (1)

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "key=HKLM\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters"
    for %%v in (FileInfoCacheLifetime FileNotFoundCacheLifetime DirectoryCacheLifetime) do (
        set "%%~v="
        for /f "tokens=3" %%a in ('reg query "%key%" /v "%%~v" 2^>nul ^| find "REG_DWORD"') do set /a "%%~v=%%a"
        if not defined %%~v (
            echo %%~v is not defined
        ) else if not !%%~v! equ 0 (
            echo %%~v is not correctly defined
        ) else (
            echo %%~v is correctly defined
        )
    )

    endlocal

Upvotes: 2

Related Questions