Solved

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

Posted on 2012-03-24
4
690 Views
Last Modified: 2012-03-24
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

0
Comment
Question by:ReneGe
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
paultomasi earned 500 total points
ID: 37761212
Line 8 should be:

    FOR /F "delims=\ " %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (

(you missed the closing double-quote after '\\')

Does it still require attention?
0
 
LVL 10

Author Comment

by:ReneGe
ID: 37761235
Thats a good one ;)

I guess I should rest this weekend...

Thanks Paul

Cheers,
Rene
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37761238
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.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37761245
No probs ReneGe, it happens to the best of us... and I've pulled my hair out and screamed on many occasions!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now