Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post

    Log results of bat file with timestamp name.

    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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    In your batch file pipe the output to a text file.

    commandtostartyourprogram > 2004.08.04-13.27.txt
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you can do it like this:
    Code:
    @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.

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

    BTW, this is the reference I used:
    http://www.experts-exchange.com/Oper..._20900979.html


    Let me know that it works out.
    Sadiq.
    Last edited by sad69; 08-05-2004 at 12:22 AM. Reason: added reference..

  • #4
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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:
    Code:
    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?

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Spookster
    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
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No idea.. where are you putting this "> file.log" line? What error are you getting?

    Sadiq.

  • #7
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    @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
    Code:
    @echo off
    
    echo "hello world"
    
    echo %0
    2004.8.4-16.45.txt
    Code:
    "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.

  • #9
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm running Windows XP SP1.. what are you running? That could be an issue....

    Sadiq.

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Morgoth
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    I am running windows pro 2000.

    If I do what Spookster says and put the "> file" after the generator:
    Code:
    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:
    Code:
    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.

  • #12
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,064
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Morgoth
    I am running windows pro 2000.

    If I do what Spookster says and put the "> file" after the generator:
    Code:
    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:
    Code:
    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:
    Code:
    perl "C:\generator\generator" > %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt
    ftp -s:%0 >> %sdy%.%sdm%.%sdd%-%sth%.%stm%.%sts%.txt
    HTH,

    -sage-
    Last edited by sage45; 08-05-2004 at 06:40 AM.
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #13
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #14
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #15
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •