Problem: The below batch program exits when trying to run the 1st for
loop, and %file3%
txt file only has:
Displayname
-------------
Need advice on why the for loop is not working please.
Partial output of REG query in 1st for loop in a text file:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Adobe AIR
DisplayName REG_SZ Adobe AIR
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Cisco AnyConnect Secure Mobility Client
DisplayName REG_SZ Cisco AnyConnect Secure Mobility Client
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CitrixOnlinePluginPackWeb
DisplayName REG_SZ Citrix Receiver
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\KLiteCodecPack_is1
DisplayName REG_SZ K-Lite Codec Pack 10.7.5 Basic
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Mobile Broadband HL Service
DisplayName REG_SZ Mobile Broadband HL Service
Desired output in %file3%:
Displayname
-------------
Adobe AIR
Cisco AnyConnect Secure Mobility Client
Citrix Receiver
K-Lite Codec Pack 10.7.5 Basic
Mobile Broadband HL Service
Batch code:
@echo OFF
setlocal enableextensions enabledelayedexpansion
set "Version_tool=v2"
Echo Version: %Version_tool%
Echo Modified 19/01/15 by xxx
SET "ComputerName=%computername%"
echo ComputerName is: %ComputerName%
SET "file3=%~dp0%computername%-programs_unsorted.txt"
echo File3 is: %file3%
If Exist %file3% Del %file3%
echo Displayname >> %file3%
echo ------------- >> %file3%
set "keyname=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
echo keyname is: !keyname!
Rem === Problem is below - program exits when running for loop ===
For /f "usebackq tokens=* delims=" %%A in (`REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" /V /F DisplayName /S /E`) do (
pause
set "DisplayName=%%A"
echo DisplayName is: !Displayname!
pause
set "Display_Substring=!DisplayName:~0,71!"
echo Display_Substring is: !Display_Substring!
pause
if not "!DisplayName:HKEY_LOCAL_MACHINE=!"=="!DisplayName!" echo !DisplayName! >> %file3%
::if !DisplayName:~0,71!==HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ (
:: echo skipping !DisplayName!
::) else (
:: echo !DisplayName! >> %file3%
::)
)
Problems:
1) You specify usebackq
, but you used single quotes, so FOR /F processes the string instead of the command. Drop the usebackq
and the loop will process the command properly. The only time I ever use usebackq
is when I am reading from a file whose path must be quoted, typically because of spaces.
2) Your IF statement is a mess:
Your command uses normal expansion instead of delayed expansion, so it cannot see the value set within the loop.
You used =
but the comparison operator is ==
.
Values should be quoted on both sides of the comparison when using delayed expansion substring operation within an IF statement, or you must escape the comma. I recommend the quotes (actually you don't even need the IF at all)
if "!DisplayName:~1,71!"=="yourValue..."
3) You want to exclude the summary line of output. The simplest solution is to pipe the output to FIND or FINDSTR and let it filter out lines you don't want. The pipe operator must be escaped within the IN() clause.
4) You can let FOR /F parse out the columns (tokens). You want the 3rd column, which contains spaces. None of the prior columns contain spaces, so you can use tokens=2*
, which means capture the 2nd token in the first variable, and preserve everything after that in the second variable.
for /f "tokens=2*" %%A in (
'REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /V /F DisplayName /S /E^|find "DisplayName"'
) do echo %%B>>"%file3%"