Redirection Echo Output into .txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chunter
Posts: 4
Joined: 30 Sep 2009 14:33

Redirection Echo Output into .txt file

#1 Post by Chunter » 07 Feb 2010 15:17

Hi, I seem to be having some trouble with redirection, here's my code:

Code: Select all

@echo off

rem This is the second batch file.
cls

color 0c


if not "%5" == "" goto toomany
if "%2" == "" goto toofew
if "%3" == "" goto calc
if "%4" == "" goto calc
goto calc

:toofew
echo Too few
goto end
:toomany
echo Too many
goto end

:calc
set /a total = 0
set /a rate = 0
set /a commision = 0

 if not "%5"=="" (
   set /a total=%5+%4+%3+%2+%1
  )else (
      if not "%4"=="" (
         set /a total=%4+%3+%2+%1
         ) else (
            if not "%3"=="" (
               set /a total=%3+%2+%1
               set numdays=3
               ) else (
                  set /a total=%2+%1
                      )
                 )
             )




if %total% leq 5000 set /a rate = 1
if %total% gtr 5000 set /a rate = 2
if %total% gtr 9999 set /a rate = 3
if %total% gtr 14999 set /a rate = 4
set /a commision = rate * total / 100

echo Total sales has been calculated as %total% >>commlog.txt
echo Commission rate has been calculated as %rate% percent >>commlog.txt
echo Total commission has been calculated as %commision% >>commlog.txt

:end
pause


The output would be something like
Total sales has been calculated as 500
Commission rate has been calculated as 1 percent
Total commission has been calculated as 5

Simply attaching ">>commlog.txt" redirects it to the file I want but there's a couple of problems with that.

It doesn't allow me to see the output on the command-line screen, which I want in conjunction with the redirected output. Also I want it to display in the text file like a log, because lets just say this program will be used multiple times I want output to be separated with a divider, I also need for the date and time to be displayed right above the output... how can I do this?

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

Re: Redirection Echo Output into .txt file

#2 Post by aGerman » 07 Feb 2010 16:37

Chunter

You can't redirect the values to a file and to your screen using the same command, but you could write two command lines. e.g.

Code: Select all

echo Total sales has been calculated as %total% >>commlog.txt
echo Total sales has been calculated as %total%


But I think that's not exactly what you need.
IMHO it would be a good option to redirect only a date-time-value and the calculated values (without additionals) to a CSV file. CSV means Comma Separated Value. Such files are linked with Excel by default.

Code: Select all

:: [...]
echo Total sales has been calculated as %total%
echo Commission rate has been calculated as %rate% percent
echo Total commission has been calculated as %commision%

>>commlog.csv echo %date% %time:~,8%,%total%,%rate%,%commision%
:: [...]


Regards
aGerman

Chunter
Posts: 4
Joined: 30 Sep 2009 14:33

Re: Redirection Echo Output into .txt file

#3 Post by Chunter » 07 Feb 2010 17:33

Unfortunately this is for an assignment, so there are certain specifications I should follow. The first suggestion might actually work, I've considered this myself but I don't know if my instructor will allow it. I'll run it by him and see what he has to say about it, thanks for the help.

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

Re: Redirection Echo Output into .txt file

#4 Post by aGerman » 07 Feb 2010 18:26

Chunter

I don't know the given specifications, but if you have to process the data later you will get a problem if you can't find a data record in a single line.

What ever. If you want to write it as a block you could do it like that

Code: Select all

:: [...]
echo Total sales has been calculated as %total%
echo Commission rate has been calculated as %rate% percent
echo Total commission has been calculated as %commision%

>>commlog.txt echo ##########################
>>commlog.txt echo %date% %time%
>>commlog.txt echo Total sales has been calculated as %total%
>>commlog.txt echo Commission rate has been calculated as %rate% percent
>>commlog.txt echo Total commission has been calculated as %commision%
:: [...]


Regards
aGerman

Post Reply