0

So here is my code

wmic product get name > "programs.txt" 
FOR /f %%a IN (programs.txt) DO (
    pause
    echo %%a
    set /p variable= Delete %%a?
    pause
    IF "%variable%" == "yes" (
        wmic product where name="%%a" call uninstall
        cls
    ) ELSE (
    cls
    )
)

I am trying to make a program where it will display the installed programs and ask if they want to uninstall them. The code runs fine up to the for loop but then it just quits. I tried it with @echo on and it just outputs the code but doesn't run it. any help would be great

[EDIT] Just to explain a bit more when I run the code everything runs fine up till the for loop and then it gets messed up. It won't even run the pause after the for loop it just ends.

MathMXC
  • 319
  • 1
  • 3
  • 14
  • what kind of quotes is that around `%%a`? Use standard-quotes (like on the line before). Besides that, you have to use [delayed expansion](http://stackoverflow.com/a/30284028/2152082) for your `variable` – Stephan May 20 '16 at 14:14
  • normal quotes and I just added delayed expansion to no avail – MathMXC May 20 '16 at 14:37

1 Answers1

1

The issue is with how you are creating your text file. WMIC output is Unicode. When the For loop tries to read it, it opens it as a ANSI (default) file. This results in odd behavior.

Instead of redirecting your output, /output it like this:

wmic /output:"uniprogs.txt" product get name 

Then you have to convert it from Unicode to ANSI/ASCII with the TYPE command like this:

TYPE uniprogs.txt > ansiprogs.txt

Now run your for loop on ansiprogs.txt and all should be good.

Tim
  • 2,701
  • 3
  • 26
  • 47
  • It works but there is a little issue. Lets say I have Google Chrome installed and when I run this %%a equals Google not Google Chrome. It stops after the space. Do you know why? [EDIT] I checked ansiprogs.txt and it does have all of them – MathMXC May 20 '16 at 16:33
  • 1
    @MathMXC because space is a standard delimiter in `for` and "tokens=1" is the default So without further instructions, only the first word is used. . Use `for /f "delims=" %%a ...` – Stephan May 20 '16 at 16:48