Querying a registry key in a batch script - batch-file

I'm using the following code to get a list of programs being run at start up, and log them to a file.
for /f "skip=2 tokens=1,2*" %%A in ('REG QUERY "HKCU\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run" 2^>NUL') do echo %%A : %%C >> Log.txt
This works with entries where the value name doesn't contain a space, but when it does, such as with "Google Update", it messes up the tokens, and %%C becomes: REG_SZ <path>, instead of just the path.
Does anyone have a better way to query the registry and log its values?

Well I got one working solution, I'd still love to see if anyone has anything better.
for /f "skip=2 tokens=*" %%A in ('REG QUERY "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" 2^>NUL') do (
set regstr=%%A
set regstr=!regstr: =^|!
for /f "tokens=1,3 delims= |" %%X in ("!regstr!") do (
echo %%X : %%Y
)
)

Version specific, works in XP, does not work in Win 7 - see comments for details.
Columns in the output are separated by tab char (0x09), so use only tab as a separator:
for /f "skip=2 tokens=1,2* delims= " %%A
This does not show well because of how markup handles white chars, but the character after delims= must be actual TAB

Here's a better way via WMI calling the Win32_StartupCommand class, results output to screen as well as a CSV file in the same folder as per script name:
#echo off
setlocal enabledelayedexpansion
cd \ & pushd "%~dp0"
if exist "%~n0.tmp" (del /f /q "%~n0.tmp")
if exist "%~n0.csv" (del /f /q "%~n0.csv")
wmic /namespace:\\root\cimv2 path Win32_StartupCommand get /all /format:csv >"%~n0.tmp"
for /f "tokens=1,2,3,4,5,6,7,8,9 usebackq delims=, skip=2" %%a in (`type "%~n0.tmp"`) do (
echo %%b, %%c >>"%~n0.csv"
echo %%b, %%c
)
if exist "%~n0.tmp" (del /f /q "%~n0.tmp")
popd & endlocal
exit /b 0`

Related

Specific use of double loop

I recently asked you guys some help for a double conditions in batch file. You guys helped me very well but I'm now struggling with a new trouble... I'm trying to optimize my script.
Previous question (How to double conditions in batch file?)
I would like to list all files with the .doc extension that are in the subfolders named on the current date only. I think I'm making a mistake on the use of this double loop.
#echo off
chcp 1252
set Pathname="D:\testDir"
set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
set logfile=%Pathname%\logs\log.txt
cd %Pathname%
d:
for /D /R %%i in (*%year%_%month%_%day%*) do (
for /R %%i %%s in (*.doc) do (
echo "file : %%s worked and does an output at %time%" >> %logfile%
)
)
Can you give me a little help? Thanks for your help and advices.
EDIT: I need to perform actions on each .doc file in folders containing the string YEAR_MONTH_DAY. But the .doc files can also be located in other subdirectories.
It could be something like that :
D:\testDir\directory1_2021_11_16\test.doc
D:\testDir\directory1_2021_11_16\test.log
D:\testDir\directory1_2021_11_16\subDirectory1\test.doc
D:\testDir\directory1_2021_11_16\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\test.doc
D:\testDir\directory1_2021_11_17\test.log
D:\testDir\directory1_2021_11_17\subDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\subSubDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\ubSubDirectory1\test.log
D:\testDir\directoryThatIDontCare\test.doc
D:\testDir\directoryThatIDontCare\test.log
D:\testDir\directoryThatIDontCare\subDirectory1\test.doc
D:\testDir\directoryThatIDontCare\subDirectory1\test.log
Any solutions ? Thanks for your time guys !
Here's an example script you can learn from, and run, to achieve what your submitted code was intending to do.
#Echo Off
SetLocal EnableExtensions
Set "CurDate="
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
Win32_LocalTime Get Day^, Month^, Year /Format:List
2^>NUL') Do For /F "Tokens=*" %%H In ("%%G") Do Set /A "_%%G + 10000"
For /F "Tokens=1,* Delims==" %%G In ('"(Set _) 2>NUL"') Do (
SetLocal EnableDelayedExpansion & If %%H Gtr 10059 (For %%I In (!%%G:~-4!
) Do EndLocal & Set "%%G=%%I") Else For %%I In (!%%G:~-2!
) Do EndLocal & Set "%%G=%%I")
Set CurDate=%_Year%_%_Month%_%_Day%
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
If Not Defined CurDate GoTo :EOF
Set "BaseLocation=D:\testDir"
PushD "%BaseLocation%" 2>NUL || GoTo :EOF
Dir "logs" /B /A:D 1>NUL 2>&1 || (MD "logs\%CurDate%" 2>NUL || GoTo :EOF)
Set "LogFile=%BaseLocation%\logs\log.txt"
For /F "EOL=? Delims=" %%G In ('Dir "*_%CurDate%" /B /A:D 2^>NUL'
) Do For /F Delims^= %%H In ('Set "PATHEXT^=" ^& %SystemRoot%\System32\where.exe
/F /R "%%G" "test.doc" 2^>NUL'
) Do (Echo file : %%~H worked and does an output at %TIME%) 1>>"%LogFile%"
If you want to know how any of it works, please use the built-in help information for each command, use the site search facility and/or your chosen search provider. I am not a private tutor, so will not be performing such a role.

findstr "^" doesn't work but "^^" does to lookup the beginning lines

My findstr /c:"^string" works in funny way.
To lookup top of each lines using findstr /r with "^", is there any way to put the same query? At least, I found where there's variable as file name, /c:"^^abc" picks up properly, but /c:"^abc" doesn't.
>type test.txt
abc
abc
>type test.bat
setlocal enabledelayedexpansion
set Myfile=test.txt
for /F "usebackq delims=" %%i in (`findstr /n /r /i /c:"^abc" "!Myfile!"`) do (
echo %%i
)
goto :eof
>test.bat
1: abc
2:abc
>
Only when I put 2 "^"s, i.e /c:"^^abc", then,
...
for /F "usebackq delims=" %%i in (`findstr /n /r /i /c:"^^abc" "!Myfile!"`) do (
...
>test.bat
2:abc
>
Or, when I put %Myfile% instead of !Myfile!, it looks working with c:"^abc". But, Myfile will vary through each run. So %variable%, or immediate_value won't work for me.
Is there any ways to put a Universal lookup (any switch available?) so that I don't need to check each time I write codes? This would reduce quite a lot of time to debug.
FYI, Replacing !Myfile! with %%x by putting one more "For" loop works with Single "^". This is the only way I can forget about "^^"against !Myfile! for now.
>type test2.bat
setlocal enabledelayedexpansion
set Myfile=test.txt
for /F "usebackq delims=" %%h in (`dir /a-d /b "!Myfile!"`) do (
for /F "usebackq delims=" %%i in (`findstr /n /r /i /c:"^abc" "%%h"`) do (
echo %%i
)
)
goto :eof
>test2.bat
2:abc
>

Get Windows version ,and edition, pass it to variable to use with findstr after

I'm noob it's my first question, but after searching tons of answers here I still can't quite get what I want. So the problem: need to get windows version ,and edition and pass it to variable to run findstr on it afterwards I get the variable with the edition however findstr can't use it to find string in txt file.
what I have done: I'm using this code to get the ver and save it as var
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
echo !winEdition!
endlocal
goto :eof
Output: Microsoft Windows 7 Enterprise
however if I run IF statement or findstr with !winEdition! var I get no result with IF and "string not found" with findstr also if I echo the var to a txt file I get "牣獯景⁴楗摮睯⁳‷湅整灲楲敳†਍" in the txt file so I think it's encoding problem, but I can't find a way to fix it.
More details: the full code suppose to take the var from the code above, search for the string in txt file and return the next line full code:
#echo OFF
setlocal EnableDelayedExpansion
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"!winEdition!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
rem Search for the lines
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof
The second part of the code works and tested with string and with var I want to automate it further. Thank you for help I am also open to alternative ways to get the result!
wmic prints some additional "empty" lines (1), which overwrites your value (watch with echo on)
#echo off
setlocal
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
if not defined winEdition set winEdition=%%G
)
echo %winEdition%
(1) on screen they appear to be empty, but technically they aren't. They contain a crippeled line break, which causes a lot of headache...
Hi again I found the answer!!! So I just used a different command to find the win version, and edition thanks to #LotPings, it's a bit slower than wmic but it works consistently every time.
Code:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=4-6" %%a in ('"systeminfo | find /i "OS Name""') do (
set "ver=%%a %%b %%c"
)
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!ver!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
if "%ver%" equ "Windows 10 Pro" echo YES
endlocal
goto :eof
Thank you all for help, and suggestions!
ps. the serials.txt is just a normal text file created with notepad encoding UTF-8 and the content is just:
Windows 10 Pro
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Windows 7 Pro...
and IF is just another check that it works in original code the problem is not with the second part nor with serials.txt because I couldn't compare the !winEdition! with IF statement.
BTW found the problem with my original solution thx to the one I found above
the problem was wrong token= solution:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=3-5 delims== " %%A in ('wmic os get Caption /value ^| find "Windows" ') do (
set "winVer=%%A %%B %%C"
)
echo !winVer!
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!winVer!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof

How to use multiple loops with excluding strings?

I am trying to make an impossible loop combination. :) What I need is exclude specific list of services from all services, and all this inside script itself (no external files).
I need somewhat combined several loops and its too complex for me I think. :)
FOR /F "tokens=1* delims=: " %%A IN ('sc query state^= all ^| FIND "SERVICE_NAME:"') DO echo %%B
FOR /F "tokens=2* delims=_" %%S IN ('"FINDSTR /B /I /C:"S_" "%~f0""') DO echo %%S
FOR %%X IN (%%B ^| "FINDSTR /B /I /V /C:"%%S"") DO ???
S_Dhcp
S_WSearch
S_XboxNetApiSvc
In first phase, loop loading services by "SERVICE_NAME:"
Second, loading list of strings (service names) recognition by "S_".
Compare and exclude with output.
There is more easy way which work, but purely out of curiosity, is this possible to make work?
findstr can check for several words, so converting your list to a line of words and use this line with findstr makes things quite easy:
#echo off
setlocal enabledelayedexpansion
REM convert list to line:
for /f "tokens=2* delims=_" %%s IN ('findstr /b "S_" "%~f0"') DO set "exclude=!exclude! %%s"
REM for each service name do:
for /f "tokens=2 delims=: " %%a IN ('sc query state^= all ^|find "SERVICE_NAME:"') do (
REM if it is not in exclude list, echo it:
echo %%a|findstr "%exclude%">nul || echo %%a
)
goto :eof
REM list of services to exclude:
S_Dhcp
S_WSearch
S_XboxNetApiSvc
S_SysMain
S_TabletInputService
S_wuauserv
S_wudfsvc
S_WwanSvc
#echo off
setlocal EnableDelayedExpansion
rem Load the list of names as a string, enclose each name in slahes
rem in order to avoid partial name matches (like NetApi in XboxNetApiSvc)
set "exclude=/"
for %%S in (
Dhcp
WSearch
XboxNetApiSvc
) do set "exclude=!exclude!%%S/"
rem For each active service
FOR /F "tokens=2 delims=: " %%A IN ('sc query state^= all ^|find "SERVICE_NAME:"') do (
rem If the name is not in the exclude list, echo it:
IF "!exclude:/%%A/=!" EQU "%exclude%" echo %%A
)

file exists with condition today's date

I have a folder where files are dumped with timestamps...
filename_ver20130405121320.csv
I wish to create a batch script that makes sure 5 files have been created with todays date.
im guessing i will need to use a for loop with a date limit of today.
FOR /r %foldername% %%g IN (*.csv) DO (
echo %%~nxg
)
using a forfiles statement lists the files, is it possible to use a counter and +=1 every time it displays a filename?
forfiles /S /P %foldername% /m *.csv /d 0
the logic is
if number of files in a foldername is less than 5 where file created is today
echo error! missing files
any help would be much appreciated
date returned on machine as Mon 22/07/2013
use this to set date
:: set date
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET dd=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET setDate=%dd%/%mm%/%yyyy%
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET yyyy=2013
SET mm=07
SET dd=22
SET count=0
FOR /f %%g IN ('dir /b /a-d *%yyyy%%mm%%dd%????.csv') DO (
SET filename=%%~ng
SET filename=!filename:~-12,-4!
if "!filename!"=="%yyyy%%mm%%dd%" SET /a count+=1
)
ECHO %count%
GOTO :EOF
I've simply set yyyy,mm,dd to constants, obviously - just poke your date-decoder in as appropriate.
Note that you could prefix the filemask with a directoryname if required - and enclose the entire filemask in "rabbit's ears" if there are spaces or other confounding characters in the resultant mask.
Important: the filemask is merely a primary filter. The dir would list a file named filename_ver2013040512132.csv for instance (1 digit missing...) so the gymnastics with the processing would still be required.
I'm also assuming relatively sane filenames. Likely ! in a filename would cause conniptions.
I came up with this and it seems to work so far
for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=5" %%G in ('dir %foldername% /a-d ^| find "%today%"') do (
set /a fileCounter += 1
echo %%G
)
echo %fileCounter%
This may work (untested): edited to check only the date in yyyymmdd format
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
for /f %%a in ('dir "*ver%datestamp%*.csv" /b /a-d^|find /c /v "" ') do (
if %%a LSS 5 echo files are missing
)

Resources