Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

Batch File[Advanced]: FOR Sub-loop does not work

Hi there,

The FOR loop between lines 13 and 17 does not work.  However, if I remove it from the FOR...%%A... loop, it works.

Could you please fix it?
And while at it, if you have ideas to improve my script, please share your wisdom.

Thanks for your help,
Rene

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET LogFile=%~dpn0.log

ECHO %date% %time% START>>"%LogFile%"
ECHO ----------------------------------->>"%LogFile%"

FOR /F "delims=\ " %%A IN ('NET VIEW ^| FINDSTR -i "\\') DO (
   SET Flag=0
   ECHO [%%A]

   REM LISTING PROGRAMS TO BE UNINSTALLED
   FOR /F "tokens=2 delims=," %%B in ('wmic /node:"%%A" product get caption^,name /format:csv ^| findstr -i "LibreOffice OpenOffice"') DO (
      SET Flag=1
      ECHO %Date% %Time% [%%A] Will be uninstalled [%%B]
      ECHO %Date% %Time% [%%A] Will be uninstalled [%%B]>>"%LogFile%"
   )

   REM UNINSTALLING UNWANTED APPS
      IF !Flag! == 1 (
         WMIC /node:"%%A" /failfast:on product where "name like 'LibreOffice%%' or name like 'OpenOffice%%'" call uninstall /nointeractive
         ECHO %Date% %Time% [%%A] Programs uninstalled [!errorlevel!]>>"%LogFile%"
      )

   REM IF MICROSOFT OFFICE IS NOT INSTALLED, INSTALL LIBRE OFFICE
      WMIC /node:"%%A" product get name | findstr -i /C:"MICROSOFT OFFICE" >NUL
      IF !errorlevel! == 0 (
         ECHO "\\%%A" [MICROSOFT OFFICE IS INSTALLED]
         ECHO %Date% %Time% [%%A] Microsoft Office is installed>>"%LogFile%"
      ) ELSE (
         ECHO \\%%A [INSTALLING LIBRE OFFICE]
         SET Folder=\\%%A\C$\Temp
         IF NOT EXIST "!Folder!" MD "!Folder!"
         
         XCOPY /Y /I "LibO_3.5.1_Win_x86_install_multi.msi" "!Folder!"
         PSEXEC \\%%A C:\Windows\System32\msiexec.exe /i "C:\Temp\LibO_3.5.1_Win_x86_install_multi.msi" -qb
         
         ECHO %Date% %Time% [%%A] LibreOffice_3.5.1 has been installed [!errorlevel!]>>"%LogFile%"
         DEL /f /q "!Folder!\LibO_3.5.1_Win_x86_install_multi.msi"
      )
   ECHO --------------------------------------------
   ECHO. >>"%LogFile%"
)

ECHO ----------------------------------->>"%LogFile%"
ECHO %date% %time% COMPLETED>>"%LogFile%"
ECHO.>>"%LogFile%"

PAUSE
EXIT

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

Thats a good one ;)

I guess I should rest this weekend...

Thanks Paul

Cheers,
Rene
You can also delete lines 17 through 20 - although, I guess this is intentional while you're testing the inner FOR-loop between lines 13 and 17.
No probs ReneGe, it happens to the best of us... and I've pulled my hair out and screamed on many occasions!