Output All Batch File Activity on both Text Log File & Console

I have a windows batch script I'm writing which helps manage and streamline a file transfer and replication process throughout several servers.  It also allows a user to delete, delete / copy, copy, and copy to additional servers within the LAN environment.

I recently wrote additional lines and appended to each line the ability to log all activity everytime this script is ran, when it is successfully executed, it displays who ran it and at what time towards the end of the log file.

My problem and question is - how can I modify the code so that I can have the output show on both the console screen while I am running it and also have it sent to the log file as well?  If I take out the log file commands towards the end, it shows up on the dos console just fine, but if I put the log file command there it just sends it to the log file and it is just blank on the dos console screen until it completes it.  I need it to show both on console and log file.

I've attached the code, assistance is appreciated.  Let me know if there are any questions.
AppScript.bat.txt
bman2011Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
You could 'tee' for this, tee will split a programs output in two, sending one copy to the screen and the second to a file. For example DIR/S | TEE DIR.TXT will redirect the output to both the screen and the file DIR.TXT

There is a tee.exe in unxutils for example.
bman2011Author Commented:
I have tried TEE in the past but have failed.  Could you possibly rewrite around 3-5 lines of the code I attached utilizing TEE to perform what I need it to do?
Bill PrewIT / Software Engineering ConsultantCommented:
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Bill PrewIT / Software Engineering ConsultantCommented:
Something like this:
@ECHO OFF

SET LogFile=A:\Apps\logfile.txt

ECHO ENTER FILE PATH - A:\Apps\CONTRACT\:
SET /P PATHVAR= 

:OPTION
ECHO -------------------------------------------------------
ECHO ^| 1 - COPY FILES          4 - COPY FILES w/FTP        ^|
ECHO ^| 2 - DELETE FILES        5 - DELETE FILES w/FTP      ^|
ECHO ^| 3 - DELETE/COPY FILES   6 - DELETE/COPY FILES w/FTP ^|
ECHO ^| Q - QUIT                                            ^|
ECHO -------------------------------------------------------
ECHO ENTER OPTION:
SET /P OPTIONVAR=

IF /I "%OPTIONVAR%"=="1" GOTO :COPY
IF /I "%OPTIONVAR%"=="2" GOTO :DELETE
IF /I "%OPTIONVAR%"=="3" GOTO :DELETE
IF /I "%OPTIONVAR%"=="4" GOTO :COPY
IF /I "%OPTIONVAR%"=="5" GOTO :DELETE
IF /I "%OPTIONVAR%"=="6" GOTO :DELETE
IF /I "%OPTIONVAR%"=="Q" GOTO :END
ECHO *** INVALID OPTION ***
GOTO :OPTION

:DELETE
ECHO Delete from AppServer started. | TEE -a "%LogFile%"

RD /S /Q "K:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER1 completed. | TEE -a "%LogFile%"

RD /S /Q "L:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER2 completed. | TEE -a "%LogFile%"

RD /S /Q "M:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER3 completed. | TEE -a "%LogFile%"

RD /S /Q "N:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER4 completed. | TEE -a "%LogFile%"

RD /S /Q "O:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER5 completed. | TEE -a "%LogFile%"

RD /S /Q "P:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER6 completed. | TEE -a "%LogFile%"

RD /S /Q "Q:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER7 completed. | TEE -a "%LogFile%"

RD /S /Q "R:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER8 completed. | TEE -a "%LogFile%"

RD /S /Q "S:\CONTRACT\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER9 completed. | TEE -a "%LogFile%"

IF /I "%OPTIONVAR%"=="3" GOTO :COPY | TEE -a "%LogFile%"
IF /I "%OPTIONVAR%"=="5" GOTO :DELETE_FTP | TEE -a "%LogFile%"
IF /I "%OPTIONVAR%"=="6" GOTO :DELETE_FTP | TEE -a "%LogFile%"
GOTO :END | TEE -a "%LogFile%"

:DELETE_FTP
ECHO Delete from FTP started. | TEE -a "%LogFile%"

RD /S /Q "Y:\webServer\Apps\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER10 completed. | TEE -a "%LogFile%"

RD /S /Q "Z:\webServer\Apps\%PATHVAR%" | TEE -a "%LogFile%"
ECHO Delete from SERVER11 completed. | TEE -a "%LogFile%"

IF /I "%OPTIONVAR%"=="6" GOTO :COPY | TEE -a "%LogFile%"
GOTO :END | TEE -a "%LogFile%"

:COPY
ECHO Transfer to SERVER1 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "K:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER1 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER2 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "L:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER2 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER3 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "M:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER3 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER4 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "N:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER4 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER5 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "O:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER5 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER6 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "P:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER6 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER7 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "Q:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER7 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER8 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "R:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER8 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER9 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "S:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER9 completed. | TEE -a "%LogFile%"

IF /I "%OPTIONVAR%"=="4" GOTO :COPY_FTP | TEE -a "%LogFile%"
IF /I "%OPTIONVAR%"=="6" GOTO :COPY_FTP | TEE -a "%LogFile%"
GOTO :END | TEE -a "%LogFile%"

:COPY_FTP 
ECHO Transfer to SERVER10 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tgz" "Y:\webServer\Apps\%PATHVAR%\*.tgz" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.txt" "Y:\webServer\Apps\%PATHVAR%\*.txt" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.zip" "Y:\webServer\Apps\%PATHVAR%\*.zip" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tar" "Y:\webServer\Apps\%PATHVAR%\*.tar" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER10 completed. | TEE -a "%LogFile%"

ECHO Transfer to SERVER11 started. | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tgz" "Z:\webServer\Apps\%PATHVAR%\*.tgz" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.txt" "Z:\webServer\Apps\%PATHVAR%\*.txt" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.zip" "Z:\webServer\Apps\%PATHVAR%\*.zip" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tar" "Z:\webServer\Apps\%PATHVAR%\*.tar" /H /S /Y /V /I /Z /R | TEE -a "%LogFile%"
ECHO Transfer to SERVER11 completed. | TEE -a "%LogFile%"

ECHO App Posted by %USERDOMAIN%\%Username% on %date% - %time% | TEE -a "%LogFile%"

:END
PAUSE

Open in new window

~bp
bman2011Author Commented:
Using tee would obviously require the need for tee.exe in addition to the batch script file to be residing on the same directory for the output to work, correct?
Bill PrewIT / Software Engineering ConsultantCommented:
tee.exe will either need to be in the current folder when the batch job runs, or in a folder that is included in the PATH variable.  I typically have a c:\utils folder or the like where I place little utility programs like this, and then I include that folder in my PATH setting so they are always available.

~bp
Paul TomasiCommented:
Your best bet is to do something like this (here's just a small example dealing with just one of your commands):
set /p =Deleting from SERVER 10... <nul
RD /S /Q "Y:\webServer\Apps\%PATHVAR%"

if errorlevel 0 (
  echo Completed.
  ECHO Delete from SERVER10 completed.>>"%LogFile%"
) else (
  echo Completed with errors!
  ECHO Delete from SERVER10 completed with errors.>>"%LogFile%"
)

Open in new window

The output from either line 5 or line 8 will be appended onto the output from line 1 after the command on line 2 executes. At the same time, the appropriate output is sent to your logfile.
Paul TomasiCommented:
You need this at the start of your code:
:GetPath
  SET /P "pathvar=ENTER FILE PATH (Deafult is: A:\Apps\CONTRACT): "

  if "%pathvar: =%"=="" set pathvar=A:\Apps\CONTRACT

  if not exist "%pathvar%\*" (
    echo Cannot find path: %pathvar%.
    pause
    goto GetPath
  )

Open in new window

Paul TomasiCommented:
Your menu should look something like this (also check out the labels for your subroutines):
:MENU
  ECHO -------------------------------------------------------
  ECHO ^| 1 - COPY FILES          4 - COPY FILES w/FTP        ^|
  ECHO ^| 2 - DELETE FILES        5 - DELETE FILES w/FTP      ^|
  ECHO ^| 3 - DELETE/COPY FILES   6 - DELETE/COPY FILES w/FTP ^|
  ECHO ^| Q - QUIT                                            ^|
  ECHO -------------------------------------------------------

:GetOPTION
  SET /P "OPTIONVAR=ENTER OPTION (1..6 or Q): "
  set optionvar=%optionvar: =%

  if "%optionvar%"=="" (
    echo Oops! Nothing entered. Please try again."
    pause
    goto GetOption
  )

  if /i "%optionvar%"=="q" goto :eof

  if "%optionvar%" geq "1" (
    if "%optionvar%" leq "6" (
      call :OPTION%optionvar%
      pause
      goto GetOption
    )
  )

  echo  *** INVALID OPTION ***
  pause
goto :GetOPTION


:OPTION1
  ::
goto :eof


:OPTION2
:OPTION3
  ::
goto :eof


:OPTION4
  ::
goto :eof


etc...

Open in new window

Notice how label OPTION2 and OPTION3 point to the same subroutine.
Paul TomasiCommented:
Your 9 XCOPYs can be done in a single FOR-loop like this:
for %%D in (K L M N O P Q R S) do (
  XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "%%D:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R>>A:\Apps\logfile.txt
)

Open in new window

However, you also want output to both the screen and a logfile so you need these additional lines of code:

NOTE: add the following statement somewhere near the top of your program:

  SETLOCAL ENABLEDELAYEDEXPANSION
set server=0
for %%D in (K L M N O P Q R S) do (
  set /a server+=1

  set /p =Transfering to SERVER!srever!... <nul
  ECHO Transfer to SERVER!server! started.>>A:\Apps\logfile.txt

  XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "%%D:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R>>A:\Apps\logfile.txt 2>&1

  if !errorlevel! equ 0 (
    echo Completed.
    ECHO Transfer to SERVER!server! completed.>>"%LogFile%"
  ) else (
    echo Completed with errors!
    ECHO Transfer to SERVER!server! completed with errors.>>"%LogFile%"
  )
)

Open in new window

Paul TomasiCommented:
Or at the very least, you need something like this:
set server=0
for %%D in (K L M N O P Q R S) do (
  set /a server+=1

  echo Transfer to SERVER!server! started.
  ECHO Transfer to SERVER!server! started.>>A:\Apps\logfile.txt

  XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "%%D:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R>>A:\Apps\logfile.txt 2>&1

  if !errorlevel! equ 0 (
    echo  Transfer to SERVER!server! completed.
    ECHO Transfer to SERVER!server! completed.>>"%LogFile%"
  ) else (
    echo Transfer to SERVER!server! completed with errors!
    ECHO Transfer to SERVER!server! completed with errors!>>"%LogFile%"
  )
)

Open in new window

Don't forget to include SETLOCAL ENABLEDELAYEDEXPANSION somewhere near the start of your code.
Paul TomasiCommented:
Your :COPY_FTP routing could look something like this:
:COPY_FTP
  for %%S in (Y Z) do (
    ECHO Transfer to SERVER%%S started.>>%logfile%
    for %%X in (tgz txt zip tar) do (
      XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.%%X" "%%S:\webServer\Apps\%PATHVAR%\*.%%X" /H /S /Y /V /I /Z /R>>%logfile%
    )
    ECHO Transfer to SERVER%%S completed.>>%logfile%
  )
goto :eof

Open in new window

Or you could wrap the whole thing up in a pair of brackets and use just one redirection '>>%logfile%' like this:
:COPY_FTP
  (
    for %%S in (Y Z) do (
      ECHO Transfer to SERVER%%S started.
      echo Transfer to SERVER%%S started.>con
      for %%X in (tgz txt zip tar) do (
        XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.%%X" "%%S:\webServer\Apps\%PATHVAR%\*.%%X" /H /S /Y /V /I /Z /R
      )
      ECHO Transfer to SERVER%%S completed.
      echo Transfer to SERVER%%S completed.>con
    )
  )>>%logfile%
goto :eof

Open in new window

In that last example, all output is sent to the logfile except line 5 and line 10 which are sent to the screen (CONsole).
bman2011Author Commented:
Paul, thank you but I have decided with the quick solution for now, although I may implement your solution in the near future since it does not require the use of a third party utility.  

Billprew and Gerwin, my only issue remaining right now is that the log file output has no line breaks and appears all clogged up in the text file.  How can I let TEE know that a new line break is in between each line?
Gerwin Jansen, EE MVETopic Advisor Commented:
Which 'tee' did you use? I tested and got line breaks like this:
c:\temp>echo Started | tee -a log.txt
Started

c:\temp>echo Stopped | tee -a log.txt
Stopped

c:\temp>type log.txt
Started
Stopped

Open in new window

I used 'tee' from unxutils (see above).
Bill PrewIT / Software Engineering ConsultantCommented:
I also tested TEE.EXE here and got line feeds after each line with no problems.

~bp
bman2011Author Commented:
I used the TEE from unuxutil and used your code billprew.  Still have the same issue in the logfile.txt that is generated.
bman2011Author Commented:
When I open the logfile on any program other then notepad, the line breaks appear fine, however notepad is where it shows all clumped up together....

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
Ah, sounds like notepad isn't smart enough to recognize a single line feed (no carriage return) as a line break.  I'll look tonight to see if there is an option to change that in the program.

~bp
Gerwin Jansen, EE MVETopic Advisor Commented:
Weird, to rule out 'other' tee applications, can you replace tee in the batch file you have with the full path to tee.exe in you unxutils folder? Also, can you test the simple echo script I posted above?
Bill PrewIT / Software Engineering ConsultantCommented:
I confirmed that the unxutils version of tee suffers from the problem you encountered, it only places a line feed at the end of lines.

Grab tee.exe out of the binaries zip linked from this page, it handles end of line properly.

http://gnuwin32.sourceforge.net/packages/coreutils.htm

~bp
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
This is generally how I do this, I create a sub function to handle all of my Echoed output, and another Sub function to handle all of my commands, and redirect their output to the same sub fucntion that handles the Echos.

Wha-lah, you get all output echoed to both the command prompt and to a log file.

Also, I took the time to simplify the batch file a bit, you coudl certainly make it tighter than what I have done, but I took out the obvious redundancies with a small number of For Loops.

-Q

:: ScriptName: AppScript.bat
:Begin-Script
@ECHO OFF
GOTO :End-Subs
:Start-Subs
	:Start-Echo
		ECHO.%*
		IF EXIST "%LogDir%" (
			ECHO.%*>>"%LogDir%\%LogFile%"
		)
		GOTO :EOF
	:End-Echo
	:Start-CMDLogged
		FOR /F "Tokens=*" %%C IN ('%*') DO CALL :Start-Echo %%~C
		GOTO :EOF
	:End-CMDLogged
:End-Subs

:Start-Variables
	SET "LogDir=A:\Apps"
	SET "LogFile=logfile.txt"
:End-Variables

:Start-Main
	CALL :Start-Echo ENTER FILE PATH - A:\Apps\CONTRACT\:
	SET /P PATHVAR= 

	:OPTION
	CALL :Start-Echo -------------------------------------------------------
	CALL :Start-Echo ^| 1 - COPY FILES          4 - COPY FILES w/FTP        ^|
	CALL :Start-Echo ^| 2 - DELETE FILES        5 - DELETE FILES w/FTP      ^|
	CALL :Start-Echo ^| 3 - DELETE/COPY FILES   6 - DELETE/COPY FILES w/FTP ^|
	CALL :Start-Echo ^| Q - QUIT                                            ^|
	CALL :Start-Echo -------------------------------------------------------
	CALL :Start-Echo ENTER OPTION:
	SET /P OPTIONVAR=
	IF "%OPTIONVAR%" EQU "q" SET "OPTIONVAR=Q"
	IF "%OPTIONVAR%"=="1" GOTO :COPY
	IF "%OPTIONVAR%"=="2" GOTO :DELETE
	IF "%OPTIONVAR%"=="3" GOTO :DELETE
	IF "%OPTIONVAR%"=="4" GOTO :COPY
	IF "%OPTIONVAR%"=="5" GOTO :DELETE
	IF "%OPTIONVAR%"=="6" GOTO :DELETE
	IF "%OPTIONVAR%"=="Q" GOTO :END
	CALL :Start-Echo *** INVALID OPTION ***
	GOTO :OPTION

	:DELETE
	CALL :Start-Echo Delete from AppServer started.
	FOR %%A IN ("K:1" "L:2" "M:3" "N:4" "O:5" "P:6" "Q:7" "R:8" "S:9") DO (
		FOR /F "Tokens=1-2 delims=:" %%B IN ("%%~A") DO (
			CALL :Start-CMDLogged RD /S /Q "%%~B:\CONTRACT\%PathVar%"
			CALL :Start-Echo Delete from SERVER%%~C completed.
		)
	)
	IF "%OPTIONVAR%"=="2" (
		CALL :Start-Echo GOTO :End-Script
		GOTO :End-Script
	)
	IF "%OPTIONVAR%"=="3" (
		CALL :Start-Echo GOTO :COPY
		GOTO :COPY
	)
	CALL :Start-Echo GOTO :DELETE_FTP

	:DELETE_FTP
	CALL :Start-Echo Delete from FTP started.

	FOR %%A IN ("Y:10" "Z:11") DO (
		FOR /F "Tokens=1-2 delims=:" %%B IN ("%%~A") DO (
			CALL :Start-CMDLogged RD /S /Q "%%~B:\webServer\Apps\%PathVar%"
			CALL :Start-Echo Delete from SERVER%%~C completed.
		)
	)

	IF "%OPTIONVAR%" NEQ "6" (
		CALL :Start-Echo GOTO :End-Script
		GOTO :End-Script
	)

	CALL :Start-Echo GOTO :COPY

	:COPY
	FOR %%A IN ("K:1" "L:2" "M:3" "N:4" "O:5" "P:6" "Q:7" "R:8" "S:9") DO (
		FOR /F "Tokens=1-2 delims=:" %%B IN ("%%~A") DO (
			CALL :Start-Echo Transfer to SERVER%%~C started.
			CALL :Start-CMDLogged XCOPY "A:\Apps\CONTRACT\%PATHVAR%" "%%~B:\CONTRACT\%PATHVAR%" /H /S /Y /V /I /Z /R
			CALL :Start-Echo Transfer to SERVER%%~C completed.
		)
	)

	IF "%OPTIONVAR%" Lss "4" (
		CALL :Start-Echo GOTO :End-Script
		GOTO :End-Script
	)
	CALL :Start-Echo GOTO :COPY_FTP

	:COPY_FTP
	FOR %%A IN ("Y:10" "Z:11") DO (
		FOR /F "Tokens=1-2 delims=:" %%B IN ("%%~A") DO (
			CALL :Start-Echo Transfer to SERVER%%~C started.
			CALL :Start-CMDLogged XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tgz" "%%~B:\webServer\Apps\%PATHVAR%\*.tgz" /H /S /Y /V /I /Z /R
			CALL :Start-CMDLogged XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.txt" "%%~B:\webServer\Apps\%PATHVAR%\*.txt" /H /S /Y /V /I /Z /R
			CALL :Start-CMDLogged XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.zip" "%%~B:\webServer\Apps\%PATHVAR%\*.zip" /H /S /Y /V /I /Z /R
			CALL :Start-CMDLogged XCOPY "A:\Apps\CONTRACT\%PATHVAR%\*.tar" "%%~B:\webServer\Apps\%PATHVAR%\*.tar" /H /S /Y /V /I /Z /R
			CALL :Start-Echo Transfer to SERVER%%~C completed.
		)
	)
	CALL :Start-Echo App Posted by %USERDOMAIN%\%Username% on %date% - %time%
:End-Main

:End-Script
PAUSE

Open in new window

Paul TomasiCommented:
I thought he was leaning towards a 'quick solution'... I think he's settled on using TEE.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Ahh, interesting, thanks Paul.

  I didn't read the thread before I posted, I only read the posted Q, and DLed his code, and strait away modified it then posted the script above.

  Personally, I hate using 3rd Party Utilities if I can just do it within the standard Operating parameters of the CMD Line.

  When I read the post I figured it was a nice chance to just spend some time re-writing code to use some of the concepts I have found more and more useful but rarely post a solution on EE Using because they aren't "important" to the Question.

  Obviously the Code could be simplified further than I did, I was more interested in preserving most of his original code form, and implementing my most used functions here.

  If the OP is already happy with a different solution then "Oh Well", I probably have a few syntax issues in my code anyway, as it was cold writing (No Testing), so I guess good on ya whoever won this one.

-Q
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:

So, I read some of the posts, and I realized that the TEE command he's been using has been causing formatting issues, so I decided to resolve that.


The attached Script should be saved as tee.bat, then your existing script with all the TEE calls will run and use this batch file instead.

(You will need to delete the TEE.exe file you already had to allow this batch file to take it's place.)

The TEE.bat file I have attached takes the same concept I used in the full version of my batch file, and changes it to replicate the functionality of the TEE unix command using only Native CMD Functionality.

Because I'm using native CMD Functionality you will not run into any issues with CR/LF not showing correctly since this is all standard Echo now.


Because you are using TEE without any path specified in your script, then the same rules apply to this batch file as do to that EXE.

(ie. If saved somewhere in the path then you can call it whenever you like by TEE or TEE.bat, otherwise it has to either be called using a full file path, or be in the same location as the batch files Running Environment**.)

(**A Double Clicked Batch File always has the "Running Environment" of the User's Home Directory, a shortcut to the file allows you to specify a specific directory to be the "Running Environment" when the shortcut is double-clicked)

:: Script Name: Tee.bat
:: Version 1.0
:Begin-Script
@SETLOCAL
@ECHO OFF
	:Start-Setup
	IF "%~1" NEQ "-a" (
		IF "%~1" NEQ "/a" (
			SET "Append=>"
			SET "FileOut=%~1"
		) ELSE (
			SET "Append=>>"
			SET "FileOut=%~2"
		)
	) ELSE (
		SET "Append=>>"
		SET "FileOut=%~2"
	)
	:End-Setup
	
	:Start-TEE
		FOR /F "Tokens=* delims=œ" %%A IN ('FIND /V "œ"') DO (
			ECHO.%%A%Append%%FileOut%
			ECHO.%%A>CON
		)
	:End-TEE
	
:End-Script
ENDLOCAL

Open in new window

bman2011Author Commented:
Apologize for my inactivity, have been handling some personal situations that's kept me off work related matters.  I will be attempting the proposed solutions this week and report back on results.
Bill PrewIT / Software Engineering ConsultantCommented:
No worries, good luck.

~bp
Paul TomasiCommented:
So, problems with the 'quick solution' eh?... Pity, because I actually thought TEE sounded like a nice little utility.

Might need to review some of my earlier posts then:

    http:#37665364 - showing how to double-up on ECHOs
    http:#37665400 - validating blank input
    http:#37665485 - good menu design
    http:#37665573 - condensing repetitive commands
    http:#37665605 - improved code
    http:#37670036 - improved COPY_FTP routine

Sometimes, good structure is better than a quick fix!
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
lol, "true", but, Paul, don't forget mine: ID: 37690667

Since I re-implemented TEE using Windows CMD batch explicitly, and since it's using native CMD functionality it works properly at creating line feeds and carriage returns when I test it in my code, and therefore is his best "Quick" option.
Paul TomasiCommented:
QCubed

I aknowledge your comment.

Surely the asker cannot want a 'quick solution' otherwise this question would have been closed long before now!

The whole thing could have been re-written in the time it's taken to implement the 'quick solution'!

Oh well, I'll keep watching this space...

I genuinely sense some shared points here based on the following statement:

    Quote: "..although I may implement your solution in the near future since it does not require the use of a third party utility.."

Will be interesting to see who gets 'ACCEPTED' and who gets 'ASSISTED'.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
Paul TomasiCommented:
no need to delete...

plenty of good info here
Paul TomasiCommented:
Points to be awarded as follows:

Rather than apply points individually to each comment and then having to deal with the odd rounded-up 3 points, might it be better to apply them by topic as in the following:

    http:#a37664978 65 pts. ASSISTED: (gerwinjansen) Suggesting TEE.EXE
    http:#a37665046 65 pts. ASSISTED: (billprew) Link to MTEE.EXE
    http:#a37687959 65 pts. ASSISTED: (billprew) Identifying problem using TEE
    http:#a37665364 196 pts. ACCEPTED: (paultomasi) Modifying code (see below)
how can I modify the code so that I can have the output show on both the console screen while I am running it and also have it sent to the log file as well?
    http:#a37687940 22 pts. ASSISTED: (bman2011) Confirming problem with Notepad
    http:#a37690667 87 pts. ASSISTED: (Qcubed) Coded TEE.BAT

(All rated 'A' or at least 'B')


EXPLANATION

Much information was posted here and although I favour the simple approach (http:#a37665364) - modifying (and simplifying) the code and directing output twice (one to the screen and one to a file) to be fair to other experts, I have divided the points as follows:

There is a total of 23 comments therefore, 500 / 23 = 21.739 rounded to 21.74

This is multipled by the number of comments posted by each expert:

    gerwinjansen: 3 comments therefore, 3 x 21.74 = 65.22 rounded to 65
    billprew: 6 comments therefore, 6 x 21.74 = 130.44 rounded to 130
    paultomasi: 9 comments therefore, 9 x 21.74 = 195.66 rounded 196
    bman2011: 1 comment therefore, 1 x 21.74 = 21.74 rounded to 22
    QCubed: 4 comments therefore, 4 x 21.74 = 86.96 rounded to 87

This gives a total of 500 points with no remainder.

    gerwinjansen 65
    billprew 130
    paultomasi 196
    bman2011 22
    QCubed 87

6 of my comments deal with simplifying the code which in turn reduces the averall amount of work required.


ACCEPTED vs ASSISTED

Using TEE sounded great at first but was later proven to be problematic. In my expert opinion, had the batch file consisted of just a simple command then there is no doubt the advice would have been to double up on the output - one to the screen and one to a file (http:#a37665364 as already stated). All I have done is scale this up to meet the needs of the rest of the batch file.


NOTE: This is a very pertinent subject matter and one which deserves to remain in EE for future reference.

I trust this meets with everone's approval.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
I Am fine with this point allocation; although division by # of comments does not normally work for point allocation (for many reasons) this is a case where it is closer to correct than a simple division.

I agree with Paul that modifying the original code to have the inherent function is best, though the author was not looking for this, all of us did or would suggest this as the proper method for solving the problem, and only because the author asked for an alternative did each of us provide another method.
Paul TomasiCommented:
QCubed

Am I reading this right? You state:
modifying the original code to have the inherent function is best, though the author was not looking for this
The author states:
how can I modify the code so that I can have the output show on both the console screen while I am running it and also have it sent to the log file as well?
I'm a bit confused by your comment, for example:
and only because the author asked for an alternative...
Alternative to what?
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Paul,

I'm not arguing with you, I am supporting you.

The author prefered to use TEE in his comment to you, as you point out in ones of your comments to me.
Paul TomasiCommented:
QCubed

 I know you're not arguing with me... Apologies if I gave you the impression I thought you were. I was merely confused.

But now that you say he prefered the TEE I know you're not referring to the original question (where he prefered to modify his code). My mistake for not thinking past the initial question itself.

Yes you are right. At first he showed a preference to using TEE and it seemed the better choice. In fact, even I liked that too.

Noone seems to have commented further on MTEE so I do not know whether that too sufers from the same problem.

I can only assume the carriage-return thing is a unix-PC compatibility issue and therefore may also be inherent in MTEE as well as TEE. Perhaps someone can confirm this is not the case with MTEE. Personally, I haven't used either so I really don't know.

And yes you are right, the author seemed to prefer a 'quick' solution once he was made aware of TEE.

Not knowing the final outcome of this question means clarification is required.

In any event, the question is an interesting one, one which I have not seen myself before and therefore, is of interest for future reference.

No doubt this issue will resurface in a future question likewise, there might be an occasion when something like TEE can prove to be very useful - such as in debugging batch files for example, sending output to a file as well as the screen while not necessitating the removal of '@echo off' at the start of the batch file thereby, preserving proper screen output.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.