1
votes

I am trying to write batch to export the items in HKLM\Software\Microsoft\Windows\CurrentVersion\Run and HKCU\Software\Microsoft\Windows\CurrentVersion\Run to a text file then to count the number of returned items and display the number.

Example: It returns Google Chrome REG_SZ xxxdataxxx to the text file then I just want it to count the instances of returned items, in this case it would be 1 item.

Thanks!

2

2 Answers

0
votes
@echo off
setlocal EnableDelayedExpansion

set OUTPUT=reg_%RANDOM%.txt
reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Run" | findstr "REG_" > %OUTPUT%
set COUNT1=0
for /f %%a in (%OUTPUT%) do (
    echo %%a
    set /a COUNT1=!COUNT1! + 1
)
echo number of items HKLM: %COUNT1%

@echo.
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" | findstr "REG_" > %OUTPUT%
set COUNT2=0
for /f %%a in (%OUTPUT%) do (
    echo %%a
    set /a COUNT2=!COUNT2! + 1
)
echo number of items HKCU: %COUNT2%
del %OUTPUT% 2> nul

set /a TOTAL=%COUNT1% + %COUNT2%
echo.
@echo total %TOTAL%
0
votes

Try like this :

@echo off
for /f %%a in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Run"') do set /a $c+=1
for /f %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"') do set /a $c1+=1
echo HKLM -^> %$c%
echo HKCU -^> %$c1%