i'm trying to loop through all installed programs:
wmic /output:C:\temp\InstallList.txt product get name
for /F "tokens=*" %%G in (C:\temp\InstallList.txt) do echo %%G
the file is created, but the output is empty!
i'm trying to loop through all installed programs:
wmic /output:C:\temp\InstallList.txt product get name
for /F "tokens=*" %%G in (C:\temp\InstallList.txt) do echo %%G
the file is created, but the output is empty!
The output is not empty. The output is in Unicode. Use more
to convert into current local encoding.
If you would like the whitespace removed from the end of the strings, see How to remove trailing and leading whitespace for user-provided input in a batch file?
setlocal enabledelayedexpansion
wmic product get name | more >"C:\temp\InstallList.txt"
for /F "skip=1 usebackq tokens=*" %%G in (`type "C:\temp\InstallList.txt"`) do (
set S=%%G
set S=!S:~0,-1!
if "!S!" NEQ "" (echo !S!)
)
Could just do it all in one
FOR /F "skip=1 delims=" %%G in ('wmic product get name') do @echo %%G
The problem is the Unicode format of the output of the wmic
command. Hence your file InstallList.txt
contains many 0x00
bytes which cause the for /F
loop to terminate.
To work around that, capture the wmic
output with a for /F
loop, so it becomes converted to non-Unicode format:
@echo off
for /F "skip=1 delims=" %%G in ('
wmic PRODUCT GET Name
') do echo %%G
The skip=1
option removes the header Name
from the output.
Unfortunately, the output still contains some disturbing artefacts (in fact, there are some orphaned carriage-returns). To get rid of them, nest another for /F
loop:
@echo off
for /F "skip=1 delims=" %%G in ('
wmic PRODUCT GET Name
') do (
for /F "delims=" %%F in ("%%G") do echo %%F
)
Of course you can parse your Unicode output file InstallList.txt
with for /F
, but you cannot do it directly, you need to use the type
command to do the conversion to non-Unicode:
for /F "skip=1 delims=" %%G in ('
type "C:\temp\InstallList.txt"
') do rem the rest is the same as above
You can also use the more
command to achieve that, by simply writing more
instead of type
.
You might notice that each line returned by the echo
command contains trailing spaces. If they disturb you, you might use the following code:
@echo off
setlocal EnableDelayedExpansion
set "REPL= "
for /F "skip=1 delims=" %%G in ('
wmic PRODUCT GET Name
') do (
for /F "delims=" %%F in ("%%G") do (
set "LINE=%%F%REPL%"
set "TRNC=!LINE:*%REPL%=%REPL%!"
call echo %%LINE:!TRNC!=%%
)
)
endlocal
What I am doing here is:
REPL
;LINE
;REPL
) and to store it in TRNC
;Of course you can do this too when parsing the file InstallList.txt
as described above.