batch regex the output of reg query command to a variable
Asked Answered
B

2

6

summary

I need to be able to find the DWORD value of a registry key and set a variable to it to run an if statement against it.

how can i grab just the dword of a reg query so that i can work with it in the rest of my script?

reg query

reg query HKLM\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall

req query output

EnableFirewall    REG_DWORD    0x1

what i need to grab

0x1

pseudo code

query firewall reg value
regex out DWORD value and set to variable var1
if var1 == 0x1 then do blah
else do other blah
Bonhomie answered 1/7, 2013 at 19:26 Comment(0)
F
11

try this:

@echo off &setlocal
for /f "tokens=2*" %%a in ('reg query HKLM\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall') do set "var=%%b"
if "%var%"=="0x1" (do this) else do that
Fuegian answered 1/7, 2013 at 19:43 Comment(0)
T
4

This should work for you:

for /f "tokens=3" %%x in ('reg query HKLM\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall') do set FWSTATUS=%%x

If testing from the command line, change the %%x to %x instead.

Toyatoyama answered 1/7, 2013 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.