...

View Full Version : Log results of bat file with timestamp name.



Morgoth
08-04-2004, 11:32 PM
I have done a search in google, and I couldn't find out how to do this.

I want to log the results on a bat file I run, and save it in a specific folder with the date and time as a name. Example: "2004.08.04-13.27.txt"

The batch file I have runs a program, which displays it's results in the open dos window, and then connects and uploads a file to and ftp server. I want to take all the information displayed on the dos file, create and store it in a log file.

I don't know how to copy what is displayed, and save it to a file. Can anyone help me, or direct me to a good article?

Thanks for your help.

Spookster
08-04-2004, 11:58 PM
In your batch file pipe the output to a text file.

commandtostartyourprogram > 2004.08.04-13.27.txt

sad69
08-05-2004, 12:20 AM
I think you can do it like this:


@echo off

set startDate=%date%
set startTime=%time%

set sdy=%startDate:~10%
set /a sdm=1%startDate:~4,2% - 100
set /a sdd=1%startDate:~7,2% - 100
set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100


batch_file.bat > ./test_bat/%sdy%.%sdm%.%sdd%-%sth%.%stm%.txt


The code in red should be replaced by the actual batch file and directory names.


The above code should be placed in its own batch file.

BTW, this is the reference I used:
http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20900979.html


Let me know that it works out.
Sadiq.

Morgoth
08-05-2004, 01:06 AM
Thanks for the code sad69, that's perfect for getting the name of the log file, but I have still have a problem.

This is what my batch file says:


goto skip

OPEN ftp.*server*.com
*username*
*password*
CD /www/
PUT "C:\pages\index.html"
QUIT

:skip

cd "C:\generator"
perl "C:\generator\generator"
ftp -s:%0


I don't know where to put the "> file.log"
Everytime I put the line "> file.log" right after "goto skip" I get an error.

I am going to mess around with this and see what happens. Any thoughts?

Spookster
08-05-2004, 01:12 AM
In your batch file pipe the output to a text file.

commandtostartyourprogram > 2004.08.04-13.27.txt

Was that not clear enough? You pipe the output from the command you use to start the program

sad69
08-05-2004, 01:15 AM
No idea.. where are you putting this "> file.log" line? What error are you getting?

Sadiq.

Morgoth
08-05-2004, 01:29 AM
I am not getting an error.

Spookster, I have a scheduler program to run the batch file, or I run it manually.
So I can't have "batch.bat > file.txt" any where, and there is a situation when I use skip.

%0 = the name of the batch file running at the moment.
ftp -s:"C:\path\batch.bat"

This reopens the batch file, so I can't keep the same log file.
This also means if I put the line "> file.txt" after "perl "C:\generator\generator"" the results will not log.

sad69
08-05-2004, 01:45 AM
I don't know that I see the problem.

The way I tested it is the code I have above is in its own file:
my_file_call.bat


@echo off

set startDate=%date%
set startTime=%time%

set sdy=%startDate:~10%
set /a sdm=1%startDate:~4,2% - 100
set /a sdd=1%startDate:~7,2% - 100
set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100


my_file > ./test_bat/%sdy%.%sdm%.%sdd%-%sth%.%stm%.txt


The actual batch file that I want to run and store its output in the timestamped file is:
my_file.bat


@echo off

echo "hello world"

echo %0


2004.8.4-16.45.txt


"hello world"
my_file


So it shows my_file and not my_file_call.

Let me know if this is still a problem, because I don't understand what you're trying to do and I don't see the problem.

Sadiq.

sad69
08-05-2004, 01:48 AM
I'm running Windows XP SP1.. what are you running? That could be an issue....

Sadiq.

Spookster
08-05-2004, 02:18 AM
I am not getting an error.

Spookster, I have a scheduler program to run the batch file, or I run it manually.
So I can't have "batch.bat > file.txt" any where, and there is a situation when I use skip.

%0 = the name of the batch file running at the moment.
ftp -s:"C:\path\batch.bat"

This reopens the batch file, so I can't keep the same log file.
This also means if I put the line "> file.txt" after "perl "C:\generator\generator"" the results will not log.


Which command inside your batch file starts up the program that outputs the information to the dos window that you want to capture? That is where you add the pipe > command.

For example if you wanted to capture the output from the dir command you would write it like this:

dir > mydirectorycontents.txt

This would output what gets printed to the dos window into the text file. So as I said before put it after the command that starts the program.

Morgoth
08-05-2004, 03:18 AM
I am running windows pro 2000.

If I do what Spookster says and put the "> file" after the generator:


perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt


The only thing put in the log file is what generator produces and not the ftp results. The only way I can get both the generator and the ftp information is by doing this:


perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt
ftp -s:%0 > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt


But that means the generator code will be writen over.

So is there aways to not write over, but add onto? This might involve a bit more code.

By the way, I just want to thank you both for the effort.

sage45
08-05-2004, 06:37 AM
I am running windows pro 2000.

If I do what Spookster says and put the "> file" after the generator:


perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt


The only thing put in the log file is what generator produces and not the ftp results. The only way I can get both the generator and the ftp information is by doing this:


perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt
ftp -s:%0 > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt


But that means the generator code will be writen over.

So is there aways to not write over, but add onto? This might involve a bit more code.

By the way, I just want to thank you both for the effort.

To prevent the generator code from being overwritten you would use this:


perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt
ftp -s:%0 >> %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt


HTH,

-sage-

Spookster
08-05-2004, 07:01 AM
Just as the almighty and all knowing Sage said use >>. The double pipes >> does an append where as a single pipe > starts writing from the beginning.

sad69
08-05-2004, 08:03 AM
Heh, I wondered what the difference was between > and >>, and now I know.

Man I love these forums! Hope you get this thing working.

Sadiq.

Morgoth
08-05-2004, 05:56 PM
sage45, thank you, that works perfectly.

Spookster, thanks for your help, I wouldn't have known the pipe command.

sad69, thank you for your date script.


Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum