Page 1 of 1
Asterisk masking in For loop: best practice?
Posted: 30 Nov 2016 08:27
by noprogrammer
Hi,
I'm working on a deletion script for Adobe software which aims at the update "nagware" (AdobeAAMUpdater-1.0 etc.).
My script also deletes two registry keys added by Acrobat on each update:
Code: Select all
@echo off
Setlocal enableExtensions enableDelayedExpansion
REM (...)
Set AcrContextItems=^
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\Adobe.Acrobat.ContextMenu^
HKLM\SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers\Adobe.Acrobat.ContextMenu
For %%? In (%AcrContextItems%) Do (
Reg query "%%?" >nul 2>&1
If !ErrorLevel! Equ 0 (
Reg delete "%%?" /f && Echo Remove Acrobat context menu entry
)
)
(The list in
%AcrContextItems% might be extended in future.)
This doesn't work due to the asterisk within the first key name and I don't know a way to make it parsable...
What's your best practice for this?
Re: Asterisk masking in For loop: best practice?
Posted: 30 Nov 2016 09:22
by SirJosh3917
I'm not entirely sure, but is it possible you could do something along the lines of
for any chars that happen to make the batch script mad?
Re: Asterisk masking in For loop: best practice?
Posted: 30 Nov 2016 09:38
by Squashman
The FOR command will always treat the asterisk as a wildcard and think you are trying to find a file name. But, a FOR /F will not. So here is a work around for you.
Code: Select all
@echo off
Setlocal enableExtensions enableDelayedExpansion
REM (...)
Set AcrContextItems=^
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\Adobe.Acrobat.ContextMenu^|^
HKLM\SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers\Adobe.Acrobat.ContextMenu
:loop
FOR /F "tokens=1* delims=|" %%G IN ("%AcrContextItems%") DO (
Reg query "%%G" >nul 2>&1
If !ErrorLevel! Equ 0 (
Reg delete "%%G" /f && Echo Remove Acrobat context menu entry
)
set "AcrContextItems=%%H"
)
IF DEFINED AcrContextItems GOTO LOOP
Re: Asterisk masking in For loop: best practice?
Posted: 30 Nov 2016 10:01
by Aacini
The same Squashman's method, but implemented in a slightly simpler way:
Code: Select all
@echo off
Setlocal enableExtensions enableDelayedExpansion
REM (...)
Set AcrContextItems=^
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\Adobe.Acrobat.ContextMenu^|^
HKLM\SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers\Adobe.Acrobat.ContextMenu
for /F "tokens=*" %%a in (^"!AcrContextItems:^|^=^
% Do NOT remove this line %
!^") do (
Reg query "%%a" >nul 2>&1
If !ErrorLevel! Equ 0 (
Reg delete "%%a" /f && Echo Remove Acrobat context menu entry
)
)
The strange FOR /F command separate the substrings delimited by a certain character into individual lines, so the processing of all lines is simpler...
And talking about "best practice": you should not use special characters in the FOR replaceable parameter; doing that just confuse the people that don't know about this FOR feature and do not provide any advantage...
Antonio
Re: Asterisk masking in For loop: best practice?
Posted: 30 Nov 2016 10:05
by Squashman
Super Nice Antonio! Did not know about that trick.
Re: Asterisk masking in For loop: best practice?
Posted: 02 Dec 2016 02:51
by jfl
You can also use the same \n variable that we use in macros. This avoids the need for the replacement trick in the for loop.
Code: Select all
@echo off
Setlocal enableExtensions enableDelayedExpansion
:# Define a LF variable containing a Line Feed ('\x0A')
set LF=^
%# The two blank lines here are necessary. #%
%# The two blank lines here are necessary. #%
:# Define a continuation line including a Line Feed
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^^" &:# Insert a LF and continue macro on next line
Set AcrContextItems=^
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\Adobe.Acrobat.ContextMenu %\n%
"HKCU\Control Panel\Desktop\PerMonitorSettings" %\n%
HKLM\SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers\Adobe.Acrobat.ContextMenu
for /F "tokens=*" %%a in ("!AcrContextItems!") do (
echo Reg query %%a
)
exit /b
Also note that the "quotes" should be in each %AcrContextItems% list item that needs them (if it contains spaces or carets, etc), not around the %%a. Else they're going to enclose the extra space around the key names in each line.
Re: Asterisk masking in For loop: best practice?
Posted: 02 Dec 2016 05:32
by Compo
None of that is of course necessary if you utilise your reg query search properly:
Code: Select all
@Echo Off
For /F "EOL=EDelims=" %%A In (
'Reg Query HKLM\SOFTWARE\Classes /S /F Adobe.Acrobat.ContextMenu /K'
) Do Reg Delete "%%A" /F>Nul
Re: Asterisk masking in For loop: best practice?
Posted: 03 Dec 2016 07:43
by noprogrammer
Thanks alot for the many solutions, I tried Aacini's version (haven't fully understood the masking yet) and it works.
The code just looks a bit "awkward" (and will get a comment).
Compo's reg query trick is another good approach - I'll check it out, too.