Simple Counter Issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Simple Counter Issue

#1 Post by atfon » 07 Dec 2021 08:08

I'm attempting what should be a relatively simple task. I'm generating a list of installed 64-bit applications and counting the number in one for /f loop. However, the generated file has an extra carriage return and the count indicates 44 lines instead of the actual number of apps (43). Here is the code I'm using:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "regQ=%__APPDIR__%reg.exe query"
set /a "numApps=1"
for /f "tokens=2*" %%a in ('%regQ% "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /s /v "DisplayName" ^|%__APPDIR__%find.exe "REG_"') do (
set /a "numApps+=1"
@echo %%b>>"%userprofile%\64BitApps.txt"
)
echo %numApps%
pause
Is there something I can do to prevent the extra line generated or properly capture the count in the one loop? I know I can capture the number of apps by different means, but I'd rather keep the code as efficient as possible.

Thanks.

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

Re: Simple Counter Issue

#2 Post by Squashman » 07 Dec 2021 08:33

How about you start counting at zero instead.

Code: Select all

set /a "numApps=0"
Otherwise you already have one app before the code even executes.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Simple Counter Issue

#3 Post by atfon » 07 Dec 2021 08:57

Squashman wrote:
07 Dec 2021 08:33
How about you start counting at zero instead.

Code: Select all

set /a "numApps=0"
Otherwise you already have one app before the code even executes.
Perfect. Thanks.

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

Re: Simple Counter Issue

#4 Post by Compo » 08 Dec 2021 13:19

What makes you think, (even if you have potentially and deliberately removed your code to determine where the end user's PC, was running a 64-bit Windows Operating System), that every application listed under that key are 64-bit too?

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Simple Counter Issue

#5 Post by atfon » 08 Dec 2021 13:53

Compo wrote:
08 Dec 2021 13:19
What makes you think, (even if you have potentially and deliberately removed your code to determine where the end user's PC, was running a 64-bit Windows Operating System), that every application listed under that key are 64-bit too?
First of all, no code was deliberately removed from this post. My intention was to provide a code example of a registry query of a location to output the data and properly update the count of the items found. It was not my intention to indicate that the registry location indicated would be the entire totality of 64-bit applications on an operating system or that every item at the location indicated was 64-bit. I simply needed assistance determining if my issue was with how my data was output or with how I formatted the counter.

[It turned out I made a simple mistake of not starting the counter at 0 as @Squashman was helpful enough to point out.]

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

Re: Simple Counter Issue

#6 Post by Compo » 08 Dec 2021 14:45

Thank you for trying to convince me that
atfon wrote:
07 Dec 2021 08:08
I'm attempting what should be a relatively simple task. I'm generating a list of installed 64-bit application
was completely irrelevant, regardless of your failure to do so.

If it wasn't relevant, why include it at all, your question would simply have read, how do I count the number of results returned from my 'for' loop?

Whilst kind of irrelevant, the command that you are using, already has a method of telling you how many matches were returned. For example:

Code: Select all

@Set "numApps=" & For /F "Tokens=1,* Delims=:" %%G In ('%__APPDIR__%reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /S /F "DisplayName" /V 2^>NUL') Do @Set /A numApps = %%H 2>NUL
@Set numApps
Please note however, that I doubt that each item listed is 64-bit, even on a 64-bit OS, and conversely that only those under 'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' are only x86. Also your code makes no attempt at determining whether it is running already from a 64-bit process, and may therefore, on a 64-bit OS, not have direct access to the subkeys you're trying to read.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Simple Counter Issue

#7 Post by atfon » 08 Dec 2021 14:59

Compo wrote:
08 Dec 2021 14:45
Thank you for trying to convince me that
atfon wrote:
07 Dec 2021 08:08
I'm attempting what should be a relatively simple task. I'm generating a list of installed 64-bit application
was completely irrelevant, regardless of your failure to do so.

If it wasn't relevant, why include it at all, your question would simply have read, how do I count the number of results returned from my 'for' loop?

Whilst kind of irrelevant, the command that you are using, already has a method of telling you how many matches were returned. For example:

Code: Select all

@Set "numApps=" & For /F "Tokens=1,* Delims=:" %%G In ('%__APPDIR__%reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /S /F "DisplayName" /V 2^>NUL') Do @Set /A numApps = %%H 2>NUL
@Set numApps
Please note however, that I doubt that each item listed is 64-bit, even on a 64-bit OS, and conversely that only those under 'HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' are only x86. Also your code makes no attempt at determining whether it is running already from a 64-bit process, and may therefore, on a 64-bit OS, not have direct access to the subkeys you're trying to read.
Compo, yes I should have left out the comment about 64-bit applications as it was really not relevant to what I was asking. I have no idea why you keep harping on a forum post which has already been resolved. The only reason to do so would be to point out imperfections in others.

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

Re: Simple Counter Issue

#8 Post by Compo » 08 Dec 2021 15:21

atfon wrote:
08 Dec 2021 14:59
I have no idea why you keep harping on a forum post which has already been resolved. The only reason to do so would be to point out imperfections in others.
Okay, just to be clear, I'm not keeping harping on about it, my one comment mentioned it, your reply, instead of just closing it out, with
atfon wrote:
08 Dec 2021 14:59
Compo, yes I should have left out the comment about 64-bit applications as it was really not relevant to what I was asking.
decided not to. My follow up comment was included for your benefit, rather than pointing out your imperfections.

Had you, instead of just quoting my last post, tried the code I posted, you may have acted less negatively towards my attempt at offering you an alternative to counting, without the additional overhead of piping each line returned through a find.exe command utility.

Nevertheless enjoy the rest of your day.

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Simple Counter Issue

#9 Post by atfon » 09 Dec 2021 07:04

I see that your code included the /F flag which also listed the DisplayName_Localized items. However, while this code generates a count of the items from the registry, it does not also output that list to a text document in the same for for loop as was my original intent.

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

Re: Simple Counter Issue

#10 Post by Compo » 11 Dec 2021 17:10

You did say the the registry example wasn't relevant, and for my own example I did say
Compo wrote:
08 Dec 2021 14:45
Whilst kind of irrelevant,<snip />

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Simple Counter Issue

#11 Post by atfon » 13 Dec 2021 07:30

Sense shines with a double luster when it is set in humility. An able yet humble man is a jewel worth a kingdom.
--William Penn

Post Reply