Easiest way to redirect content to (2) seprate files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Easiest way to redirect content to (2) seprate files?

#1 Post by SIMMS7400 » 12 Nov 2017 15:18

Hi Folks -

Without downloading a utility or using a for loop, are there any tricks to get an ECHO to go to (2) separate files in one line?

For instance, here is a portion of my script:

Code: Select all

::-- Begin Script Processing --::
>>%LOGFILE% (
ECHO ********************************************************
ECHO %~n0 Starting at %TIME%                		   	    
ECHO ********************************************************
ECHO.
ECHO ********************************************************
ECHO Login to PBCS                                          
ECHO ********************************************************
ECHO.
ECHO Action   : Login
ECHO Login    : %LOGINID%
ECHO Password : %PASSWORD%
ECHO PBCS URL : %URL%
ECHO Domain   : %DOMAIN%
ECHO --------------------------------------------------------
ECHO.
)

ECHO Login to PBCS...
PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate login %LOGINID% %PASSWORD% %URL% %DOMAIN% >nul 2>&1 && (

	ECHO PBCS Login^: Successful >>%LOGFILE%
	) || (
	ECHO PBCS Login^: Failed >>%LOGFILE%
    SET "ERR1=T"& GOTO END
)
CLS
In the fail section "||", I want to add my error file there as well. However, when doing the following ECHO PBCS Login^: Failed >>%LOGFILE% >>%ERRORFILE% the content only exists in the %ERRORFILE%.

Is there a way to get it to go to both? I'd like to try to avoid the following unless it's not possible:

Code: Select all

FOR %%F IN ( %ERRORFILE% %LOGFILE%) DO ECHO PBCS Login^: Failed >>%%F

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to redirect content to (2) seprate files?

#2 Post by aGerman » 13 Nov 2017 05:57

Either I don't understand you requirement or you are thinking quite a little too complicated.

Code: Select all

>>"abc.txt" echo foo
>>"def.txt" echo foo
What about that ...?

Steffen

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Easiest way to redirect content to (2) seprate files?

#3 Post by SIMMS7400 » 13 Nov 2017 06:19

Hi Steffen -

Most certainly, but was hoping to keep it one line.

Instead, I went with this approach and used a for loop and put most of the command in a variable so it was neater:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: Full_Backup.bat                                            --::
::                                                                           --::
::-- Description: Perform full backup for PBCS Environment                   --::
::--																		 --::
::  																		 --::
::--  Calls:      env.cmd                                                    --::
::--  Called By:  Not Applicable                                             --::
::               														   	 --::
::-- Parameters:  Call _env.cmd to get variable to determine:                --::
::               														   	 --::
::--              1. Path variables                                          --::
::--              2. User ID(s)                                              --::
::--              3. Logon(s)                                                --::
::--              4. Password(s)									         --::
::--              5. Server(s)								         	     --::
::--              6. Application(s)	    						             --::
::--			  7. Database(s)   		    							     --::
::--			  8. etc													 --::
::  																		 --::
::-- Author:	  							     --::
::-- Date:	                               	         		     --::
::  																		 --::
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set Working Directory as Script Path --::

CD /D "%~dp0"

::-- Call Environment File --::

CALL C:\Automation\Config\env.bat 

::-- Set Process Output Variables --::

SET "PLOGPATH=Full_Backup\Full_Backup_Logs\"
SET "PERRORPATH=Full_Backup\Full_Backup_Errors\"

::-- Establish INTRAPATH variables & directories --::

SET "INTRAPATH=%LogPath%\%PLOGPATH%"
SET "ERRORINTRAPATH=%LogPath%\%PERRORPATH%"

FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" ) DO IF NOT EXIST "%%~F" MKDIR "%%~F"

::-- Parse DOS TIME variable in saveable format --::

FOR /F "tokens=1-2 delims=/:" %%A IN ("%TIME%")   DO ( SET TIMESTAMP=%%A%%B)
FOR /F "tokens=* delims= " %%C IN ("%TIMESTAMP%") DO ( SET TIMESTAMP=%%C)
SET "DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%"

::-- Establish Log & Error files --::

SET "LOGFILE=%INTRAPATH%%DATETIME%_%~n0.log"
SET "ERRORFILE=%ERRORINTRAPATH%%DATETIME%_%~n0.err"

::-- Perform Additional Variable Assignment --::
SET /A "CNT=0"
SET /A "CNT+=1"
SET "TCNT=12"
SET "ERR1="

SET "DELAY="
SET "CMD=FOR %%F IN ( "%ERRORFILE%" "%LOGFILE%" ) DO"

::-- Begin Script Processing --::
>>%LOGFILE% (
ECHO ********************************************************
ECHO %~n0 Starting at %TIME%                		   	    
ECHO ********************************************************
ECHO.
ECHO ********************************************************
ECHO Step %CNT% of %TCNT%^: Login to PBCS                                          
ECHO ********************************************************
ECHO.
ECHO Action   : Login
ECHO Login    : %LOGINID%
ECHO Password : %PASSWORD%
ECHO PBCS URL : %URL%
ECHO Domain   : %DOMAIN%
ECHO --------------------------------------------------------
ECHO.
)

ECHO Step %CNT% of %TCNT%^: Login to PBCS...
PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate login %LOGINID% %PASSWORD% %URL% %DOMAIN% >nul 2>&1 && (

	ECHO PBCS Login : Successful >>%LOGFILE%
	SET /A "CNT+=1"
	) || (
	%CMD% ECHO PBCS Login : Failed>>%%F
    SET "ERR1=T"& GOTO :END
)
Thank you, though!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to redirect content to (2) seprate files?

#4 Post by aGerman » 14 Nov 2017 05:48

I see. There are TEE-like workarounds but those would actually waste more ressources and time as the FOR loop does that you used.

Steffen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Easiest way to redirect content to (2) seprate files?

#5 Post by pieh-ejdsch » 15 Nov 2017 16:23

Hello,
You can also use the redirect.
Distribute these to the expenses, which are initially redirected to - depending on what is needed.

echo is used in the macro at the for-loop and unfortunately creates a space at the end of the line.

Code: Select all

:@echo off
setlocal
:: define LF as a Line Feed (newline) character
set ^"LF=^

^" Above empty line is required - do not remove
::---------------------------
:: define macros
:: define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

set pipe2=^|@ 2^>nul 3^>nul 4^>nul (^>"%temp%\pipefile" find/v""%\n%
 ^>^&2 type "%temp%\pipefile"%\n%
 ^>^&3 type "%temp%\pipefile"%\n%
 ^>^&4 type "%temp%\pipefile"%\n%
 type "%temp%\pipefile"%\n%
)

set pipe4=^|@ for /f tokens^^^^^^=*eol^^^^^^= %%M in ('find/v""')do 2^>nul 3^>nul 4^>nul (%\n%
>^&2 echo(%%M%\n%
>^&3 echo(%%M%\n%
>^&4 echo(%%M%\n%
echo(%%M%\n%
)

echo(Message: 1%pipe2% 3>dateiMessage.txt >nul

echo(Message: 2%pipe2% 3>datei1.txt 2>datei2.txt 3>>dateiMessage.txt

echo(ERROR abcde%pipe4% 2>ERROR.txt 
pause
Phil

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to redirect content to (2) seprate files?

#6 Post by aGerman » 16 Nov 2017 10:50

Yes Phil, but that's exactly what I meant. You have a pipe in place that leads to create separate cmd.exe processes for both sides of the pipe. And you have to use e.g. FIND which is another additional process. That's the reason why I wouldn't recomment it. It's just less effective than a simple FOR loop.

Seffen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Easiest way to redirect content to (2) seprate files?

#7 Post by pieh-ejdsch » 17 Nov 2017 16:41

Yes you are right.
The redirection alone is of course better than writing the file as well as reading the file twice and writing to two more files.
Two cmd instances are of course not the true one.

But if a macro is used to provide only the redirection to these two files - I would like to create this macro so that it only provides this redirection.

Now the macro describes exactly what it does.
it only provides the redirect to two files.
The good thing is that this macro does not have to be shared.
Other handles can also be included in this redirection.
Regardless of the command output.
the ugly last space is also gone.

Code: Select all

setlocal
set Errorfile="D:\Errorfile.txt"
set   Logfile="D:\Logfile.txt"

set ">>Log+Error=for %%0 in (%Errorfile% %logfile%) do >>%%0"

%>>Log+Error% echo test 123456 xyz
%>>log+error% 2>&1 dir ...
Phil

Post Reply