Page 1 of 1

File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 21 Aug 2019 15:53
by PAB
Good evening,

I am trying to . . .

[1] Output a .txt file of the list of the installed programs [like those in Programs and Features].
[2] Delete [exclude] any duplicates in the file.
[3] Sort the file.
[4] Output the file to the desktop.

I have managed to do all of the above, BUT . . .

[1] It outputs some of the data in the cmd prompt [which I DON'T want].
[2] In the cmd prompt it says String count error: (0 does not equal 1) but it doesn't seem to have an impact on the final file which works!

Code: Select all

@echo off

regedit /E C:\RegX1.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
regedit /E C:\RegX2.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall"
regedit /E C:\RegX3.txt "HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

Find "DisplayName" C:\RegX1.txt > C:\SoftProgs.txt
Find "DisplayName" C:\RegX2.txt >> C:\SoftProgs.txt
Find "DisplayName" C:\RegX3.txt >> C:\SoftProgs.txt

For /f "tokens=2 delims==" %%a in (C:\SoftProgs.txt) Do echo %%~a >> C:\InstalledProgs.txt

Type nul>C:InstalledProgs_Final.txt
For /f "tokens=* delims=" %%a In (C:\InstalledProgs.txt) Do (
    FindStr /ixc:"%%a" C:InstalledProgs_Final.txt || >> C:InstalledProgs_Final.txt echo.%%a
)
rem ) > NUL  <<<=========================

Sort InstalledProgs_Final.txt > %userprofile%\Desktop\InstalledProgs_Final1.txt

Del C:\RegX1.txt
Del C:\RegX2.txt
Del C:\RegX3.txt
Del C:\SoftProgs.txt
Del C:\InstalledProgs.txt
Del InstalledProgs_Final.txt

Pause
[1] EDIT: On occasions, I will find the output of the duplicates in the cmd prompt quite useful. I found a way of stopping the output to the cmd prompt [if needed] by using the bit of code next to <<<=========================.

[2] I would like however, to stop or suppress the String count error: (0 does not equal 1) message in the cmd prompt, is there an easy way to achieve this please?

[3] The code is a bit of a mess really [a lot of C:\'s] , is there a neater way to do this please?

EDIT:

There seems to be a blank/empty line at the bottom of the .txt file. Would this cause the error String count error: (0 does not equal 1)?

Any help will be greatly appreciated.

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 25 Aug 2019 13:33
by penpen
I couldn't reproduce your "String count error: (0 does not equal 1)" error, sorry.

You could add some "debug" messages by just adding echoes into your code to see which command produces that message; example:

Code: Select all

@echo off
echo ###  1 ###

regedit /E RegX1.txt "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall"
echo ###  2 ###
regedit /E RegX2.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall"
echo ###  3 ###
regedit /E RegX3.txt "HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
echo ###  4 ###
:: ...

penpen

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 26 Aug 2019 02:25
by PAB
Thanks for the reply penpen, it is appreciated.
I did as you suggested and the error appears to be instigated by the line . . .

Code: Select all

FindStr /ixc:"%%a" C:InstalledProgs_Final.txt || >> C:InstalledProgs_Final.txt echo.%%a
Thanks in advance.

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 26 Aug 2019 05:43
by PAB
UPDATE: I think I have solved it!!!

On the line . . .

Code: Select all

For /f "tokens=2 delims==" %%a in (C:\SoftProgs.txt) Do echo %%~a >> C:\InstalledProgs.txt
. . . you need a . [full stop] after the echo followed by a space [important to get rid of the error appearing]!

Code: Select all

For /f "tokens=2 delims==" %%a in (C:\SoftProgs.txt) Do echo. %%~a >> C:\InstalledProgs.txt
I thought it had something to do with there being an extra empty line or something similar. I added extra code that only copied the lines with data in and then pasted them into a new file and saved it. That didn't work! I tried deleting the last row that was empty but still existed in the compiled file and that didn't work! I tried several other things that also didn't work!

The above works. The only oddity is that it puts a space at the beginning of the command prompt output for the replicated files and also includes a space at the beginning of each line in the text file. Not a biggie but annoying!

Does anyone know how to streamline the code so that there are not so many actions that need to be performed but at the same time produces the correct results please [like do I need all the C:\ 's before the file name etc].

Thanks in advance.

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 28 Aug 2019 09:29
by Eureka!
PAB wrote:
26 Aug 2019 05:43
Does anyone know how to streamline the code so that there are not so many actions that need to be performed but at the same time produces the correct results please [like do I need all the C:\ 's before the file name etc].
This is what I wrote a couple of years ago. No guarantees, not tested (recently) but maybe it can help you:

Code: Select all

@setlocal
@echo off
rem echo on

pushd "%~dp0"
set OUTPUT=software.csv
echo.DisplayName;Version;InstallDate;InstallLocation; > "%OUTPUT%"
del "tempdummy.txt"

for  %%a in ( ^
 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall ^
 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall ^
 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall ^
 HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
) DO CALL :SOFTWARE %%a

sort "tempdummy.txt" >> "%OUTPUT%"
del "tempdummy.txt"
echo.________________________________________________________ & echo.
type "%OUTPUT%"
echo.________________________________________________________ & echo.
echo. Output saved in "%OUTPUT%"
pause
goto :EOF


:SOFTWARE
   echo == %1
   for /f "usebackq tokens=1,2,* delims=" %%g in (`reg query "%1"`) DO call :READ "%%g"
goto :EOF

 
:READ
   for /f "usebackq tokens=1,2,* delims= " %%x in (`reg query %1`) DO set __%%x=%%z
   IF defined __DisplayName IF /I "%__SystemComponent%" NEQ "0x1" if NOT DEFINED __ParentDisplayName echo %__DisplayName%;%__DisplayVersion%;%__InstallDate%;%__InstallLocation%; >> "tempdummy.txt"

:: Unset the temp __vars
   rem for /f "usebackq delims==" %%x in (`set __`) DO set %%x=  2>nul
   (for /f "usebackq delims==" %%x in (`set __`) DO set %%x=) 2>nul
goto :EOF


Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 29 Aug 2019 05:44
by PAB
Thanks Eureka!, it is appreciated.

It does work OK by the way. I will have a play around with it and see if I can get the output into a .txt file formatted as a table.

Thanks again.

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 30 Aug 2019 06:44
by PAB
Can somebody please explain what the underscores are in this line of code please . . .

Code: Select all

If defined __DisplayName If /I "%__SystemComponent%" NEQ "0x1" If NOT DEFINED __ParentDisplayName echo %__DisplayName%
. . . and what exactly this does please . . .

Code: Select all

:: Unset the temp __vars
   (for /f "usebackq delims==" %%x in (`set __`) DO set %%x=) 2>NUL
Thanks in advance.

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 30 Aug 2019 09:53
by Eureka!
For every program there are multiple settings in the registry, like DisplayName, DisplayVersion, ..
But not all programs have all settings defined (on my system this varies wildly from 0 to 30 settings).
That's why the variables containing these settings have to be 'un-set' before the next program is analyzed: to prevent 'leaking' variables to the next program.

The easiest way (I could think of) was defining these vaiables wit a "__" prefix, so that they all could be 'un-set' with a single command: for /f "usebackq delims==" %%x in (`set __`) DO set %%x=.
( "set __" lists all variables that start with "__", like "set p" lists all variables that start wih "p" )

HTH ...

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 30 Aug 2019 10:06
by PAB
Brilliant, thank you Eureka! for the explanations and your time to explain them, it is really appreciated.

Have a great weekend!

Re: File partly outputs to cmd prompt. String count error: (0 does not equal 1)

Posted: 30 Aug 2019 10:54
by Eureka!
No problem; you're welcome!