2014-04-23 101 views
1

我想檢查一些.bat文件中的一些Windows註冊表項及其值的存在。檢查Windows批處理註冊表值

到目前爲止,我設法檢查是否存在:

@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) 

我怎麼能現在檢查三個值(FileInfoCacheLifetime,FileNotFoundCacheLifetime,DirectoryCacheLifetime)的值都設置爲零?

+0

http://stackoverflow.com/questions/11874598/querying-a-registry-key-in-a-batch-script?rq=1 – DNamto

+0

FOR/F 「跳過= 2個令牌= 3」 % A('Reg query「HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ User Shell Folders」/ v「Desktop」')爲/ f「usebackq tokens = 2 * delims =」設置doc =%A %i IN('dir「%doc%」/ a/s^| findstr/i/v「\ /」')DO @echo%j&echo。 –

回答

2
@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