Asterisk masking in For loop: best practice?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Asterisk masking in For loop: best practice?

#1 Post by noprogrammer » 30 Nov 2016 08:27

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?

SirJosh3917
Posts: 36
Joined: 02 May 2016 18:59

Re: Asterisk masking in For loop: best practice?

#2 Post by SirJosh3917 » 30 Nov 2016 09:22

I'm not entirely sure, but is it possible you could do something along the lines of

Code: Select all

SET SANITIZED=%UNCLEAN:^&=^_%

for any chars that happen to make the batch script mad?

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Asterisk masking in For loop: best practice?

#3 Post by Squashman » 30 Nov 2016 09:38

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

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Asterisk masking in For loop: best practice?

#4 Post by Aacini » 30 Nov 2016 10:01

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

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Asterisk masking in For loop: best practice?

#5 Post by Squashman » 30 Nov 2016 10:05

Super Nice Antonio! Did not know about that trick.

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Asterisk masking in For loop: best practice?

#6 Post by jfl » 02 Dec 2016 02:51

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.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Asterisk masking in For loop: best practice?

#7 Post by Compo » 02 Dec 2016 05:32

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

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Asterisk masking in For loop: best practice?

#8 Post by noprogrammer » 03 Dec 2016 07:43

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.

Post Reply