Teeing to a script that contains CR and BS

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Teeing to a script that contains CR and BS

#1 Post by Matt Williamson » 19 Mar 2015 11:24

I'm using the following code to make a countdown timer and a spinner in my script:

Code: Select all

:: Creates an install spinner to show  ::
:: activity for long running processes ::
:InstallSpinner
@echo off
setlocal enabledelayedexpansion
For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set n=0
:loop
tasklist /FI "Imagename eq %~1" /FO CSV /NH 2>NUL | find /i "%~1" > NUL
if errorlevel 1 goto endProg
    set /A "n=(n+1)%%4"
    set /P "=Installing %~2 Please wait.. !busy:~%n%,1!!CR!" < NUL
    PING -n 1 127.0.0.1 > NUL
goto :Loop
:endProg
echo.
echo %~2 installed. & exit /b 0


Code: Select all

:: Countdown timer ::
:Countdown
setlocal enabledelayedexpansion
For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set /A n=0
For /L %%i in (%1,-1,0) Do (
   Set /P "=Closing %~2 in %%i seconds. Please Save your work. !CR!"<NUL:
   PING -n 2 127.0.0.1 >NUL:
)
exit /b


What I'd like to do is tee out to a log file but exclude using those for just the log file but continue to display them on the screen. Here is the vbs code I'm using for the Tee:

Code: Select all

Dim str
Do While Not WScript.StdIn.AtEndOfStream
   str = WScript.StdIn.ReadLine
   WScript.StdOut.WriteLine str
   WScript.StdErr.WriteLine str
Loop


and I call it like this:

Code: Select all

@echo off
setlocal
cd /d %~dp0
Call :GetDateTime yyyy mm dd hh min ss
for /f %%a in ("%0") do (set fn=%%~na)
_.cmd | cscript //nologo tee.vbs > .\logs\%yyyy%_%mm%_%dd%_%hh%_%min%_%username%.log

exit /b

:GetDateTime Year Month Day Hour Minute Second
@echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
     IF "%~1" NEQ "" set "%~1=%YYYY%"
     IF "%~2" NEQ "" set "%~2=%MM%"
     IF "%~3" NEQ "" set "%~3=%DD%"
     IF "%~4" NEQ "" set "%~4=%HH%"
     IF "%~5" NEQ "" set "%~5=%Min%"
     IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b


And here is the entire code for the whole project:

Code: Select all

@echo off
setlocal

cd /d %~dp0
Color 0A
cls
Title Java Hermes Capitalware Installation.
set "progs=iexplorer.exe,iexplore.exe,firefox.exe,chrome.exe,javaw.exe,jqs.exe,jusched.exe"
call :GetOSVer os sp
echo.
Echo You are Running %os% %sp%
echo.

if exist "C:\Program Files\Java" (
      echo Java Installation found.
      goto :HermesInstall
 ) ELSE ( call :FindJRE jre
)

:HermesInstall
If not exist "C:\Program Files\HermesJMS\bin\hermes.bat" (
   echo.
   ECHO Installing HermesJMS.
   echo.
   call :sleep 2
   if not exist c:\.hermes mkdir c:\.hermes>nul
   if not exist "C:\MQ Files\mq jar files" mkdir "C:\MQ Files\mq jar files">nul
   copy /y hermes-config.xml c:\.hermes>nul
   copy /y "G:\ITF\Hermes Install\mq jar files\*.jar" "C:\MQ Files\mq jar files">nul
   java -jar hermes-installer-1.14.jar autoinstall.xml
) ELSE ( Echo Hermes already installed. Checking for Capitalware...
)

:CapitalWareInstall
if not exist "C:\Capitalware" (
   echo.
   echo Installing Capitalware..
   echo.
   Call :sleep 2
   mqce-setup.exe /SILENT /SUPPRESSMSGBOXES /NORESTART
)

Echo All done. This window will close in 5 seconds.
call :sleep 5
exit /b

:: Functions ::
      
:: Countdown timer ::
:Countdown
setlocal enabledelayedexpansion
For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set /A n=0
For /L %%i in (%1,-1,0) Do (
   Set /P "=Closing %~2 in %%i seconds. Please Save your work. !CR!"<NUL:
   PING -n 2 127.0.0.1 >NUL:
)
exit /b

:FindJRE
:: Locate existing Java Runtime Environment ::
:: or install if not found.                 ::
@echo off
setlocal enabledelayedexpansion
set "aux1= "
set "KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=4 tokens=3" %%A IN (
   `REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>nul`
   ) DO ( set "Val=%%~A" )
      if defined Val (
            echo.the current Java runtime is %Val%
      ) else (
            echo.Java Runtime Environment not found.
                  for /f %%a in ('dir /b jre-7u51*') do (
                        Echo.Attempting to install Java Runtime Environment.
                        set "loc=%%a"
                              If defined loc (
                                 echo.Flipping the junkware bit
                                 REG add HKLM\SOFTWARE\JavaSoft /v Sponsors /t REG_SZ /d Disable /f>nul
                                 echo Installing JRE now.
                                 call :KillProgs %progs%
                                 start "" !loc! /s WEB_JAVA_SECURITY_LEVEL=VH SPONSORS=0
                                 call :InstallSpinner !loc! "Java Runtime Environment"
                                 Set Key2="HKLM\SOFTWARE\JavaSoft"
                                    FOR /F "usebackq skip=4 tokens=3" %%G IN (
                                       `REG QUERY !Key2! /v InstallStatus 2^>nul`) do (
                                          If "%%G" EQU "decline" (
                                             Echo JRE install cancelled
                                             Endlocal&Set %~1=dec&exit /b 1
                                          )
                                    )

                              ) ELSE (
                                    Echo.Java installer not found. Please download
                                    echo.Java Runtime Environment jre-7u51-windows-i586.exe
                                    echo.from the Oracle Website and re-run this script.
                                    exit /b 1
                              )       
                  )
      )
Call :SetJavaHome
exit /b

:: Get Operating System name and service pack ::
:GetOSVer
@echo off
set key="HKLM\Software\Microsoft\Windows NT\CurrentVersion"
for /f "skip=4 tokens=3 delims=   " %%a in (
  'reg query %key% /v ProductName') do (
   set "%~1=%%a"
)
for /f "skip=4 tokens=3 delims=   " %%a in (
  'reg query %key% /v CSDVersion') do (
   set "%~2=%%a"
)
exit /b

:: Creates an install spinner to show  ::
:: activity for long running processes ::
:InstallSpinner
@echo off
setlocal enabledelayedexpansion
For /f %%a in ('copy /Z "%~dpf0" nul') Do set "CR=%%a"
Set "busy=|/-\"
Set n=0
:loop
tasklist /FI "Imagename eq %~1" /FO CSV /NH 2>NUL | find /i "%~1" > NUL
if errorlevel 1 goto endProg
    set /A "n=(n+1)%%4"
    set /P "=Installing %~2 Please wait.. !busy:~%n%,1!!CR!" < NUL
    PING -n 1 127.0.0.1 > NUL
goto :Loop
:endProg
echo.
echo %~2 installed. & exit /b 0

:: kills list of running programs        ::
:: so Java can install without hanging.  :: 
:KillProgs list
for %%a in (%*) do (
   tasklist|find "%%a">nul && (
      Call :Countdown 30 %%a
      Echo.
      Taskkill /T /F /IM %%a
   ) || ( Echo %%a not running. )
)
exit /b

:: Sets the JAVA_HOME path in the registry ::
:SetJavaHome
@echo off
setlocal enabledelayedexpansion
set "aux1= "
set "KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
FOR /F "usebackq skip=4 tokens=3" %%A IN (
   `REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>nul`
   ) DO ( set "Val=%%~A" )
      if defined Val (
            echo.the current Java runtime is %Val%
      )
FOR /F "usebackq skip=4 tokens=3*" %%C IN (
   `REG QUERY "%KEY_NAME%\%Val%" /v JavaHome 2^>nul`
) DO (
   set aux1="%%~C %%~D"
)   
set "key=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Echo Adding Java_Home environment variable
reg add "%key%" /v JAVA_HOME /t REG_SZ /d %aux1% /f >nul
For /f "skip=4 tokens=3*" %%a in ('reg query "%key%" /v JAVA_HOME') do (
   Echo Java home path set to: %%a %%b
   endlocal & setenv -m JAVA_HOME "%%a %%b"&& exit /b 0 || exit /b 1
)
exit /b

:: Pause program execution for n seconds ::
:Sleep n
@echo off
Ping -n %1 127.0.0.1 >nul
exit /b


Setenv.exe can be found here: http://barnyard.syr.edu/~vefatica/#SETENV

I'm open to any suggestions on the Teeing or the entire project.

thanks

-Matt


Post Reply