locked
How to code this batch file? RRS feed

  • Question

  • I would like to add current time, date and message at the top of this text file C:\log.txt, as shown below, so whenever I run this batch file, it will add those content at top of this file.

    Does anyone have any suggestions?

    Thanks in advance for any suggestions

    For example

    10:36 PM 01-Apr-13 Hello World

    9:36 PM 01-Apr-13 Hello World

    8:36 PM 01-Apr-13 Hello World

    7:36 PM 01-Apr-13 Hello World

    ....


    Thanks in advance for any suggestions

    Monday, April 1, 2013 2:39 PM

Answers

  • echo %time% %date% Hello there>c:\tempfile.txt
    copy /y c:\logfile.txt c:\tempfile1.txt
    copy /y c:\tempfile.txt+c:\tempfile1.txt c:\logfile.txt
    type c:\logfile.txt

    David Candy

    • Marked as answer by oemMicrosoft Tuesday, April 2, 2013 8:51 AM
    Tuesday, April 2, 2013 8:34 AM

All replies

  • Write it out to a temporary file then join the files with copy
     
    copy tempfile+Logfile.log logfile.log
     
    If you want it at the end of the file
     
    echo %time% %date% >>logfile.log 
     
    If the first line in Notepad is .LOG then it appends the date and time when opened.

    --
    .
    --
    "oem7110" wrote in message news:062e0c93-6c31-4031-b122-297f4110535a...

    I would like to add current time, date and message at the top of this text file C:\log.txt, as shown below, so whenever I run this batch file, it will add those content at top of this file.

    Does anyone have any suggestions?

    Thanks in advance for any suggestions

    For example

    10:36 PM 01-Apr-13 Hello World

    9:36 PM 01-Apr-13 Hello World

    8:36 PM 01-Apr-13 Hello World

    7:36 PM 01-Apr-13 Hello World

    ....


    Thanks in advance for any suggestions


    David Candy
    Monday, April 1, 2013 10:13 PM
  • Could you please tell me what is inserted into temp file and how to delete it after copy? and how can I add "Hello World" after date and time?

    copy C:\tempfile.txt+C:\logfile.txt C:\logfile.txt
    echo %time% %date% >>C:\logfile.txt

    Do you have any suggestions?

    Thanks you very much for any suggestions


    Thanks in advance for any suggestions


    Tuesday, April 2, 2013 1:31 AM
  • copy "%temp%\tempfile"+Logfile.log logfile.log
    Del "%temp%\tempfile"
    If you want it at the end of the file
     
    echo %time% %date% Hello World>>logfile.log 

    David Candy

    Tuesday, April 2, 2013 6:47 AM
  • I have tried that the following statement cannot add current data and time at the top of text file.

    Do you have any suggestions?

    Thanks you very much for any suggestions

    echo %time% %date% >>C:\tempfile.txt
    copy C:\tempfile.txt+C:\logfile.txt C:\logfile.txt
    Del C:\tempfile.txt


    Thanks in advance for any suggestions


    Tuesday, April 2, 2013 7:00 AM
  • echo %time% %date% Hello there>c:\tempfile.txt
    copy /y c:\logfile.txt c:\tempfile1.txt
    copy /y c:\tempfile.txt+c:\tempfile1.txt c:\logfile.txt
    type c:\logfile.txt

    David Candy

    • Marked as answer by oemMicrosoft Tuesday, April 2, 2013 8:51 AM
    Tuesday, April 2, 2013 8:34 AM
  • Could you please tell me what type c:\logfile.txt do in this case?

    Will it be possible to display time in this format hh:mm:ss without decrimal (.54) in following case?

    Do you have any suggestions?

    Thanks you very much for any suggestions

    16:49:46.54 02-Apr-13 Hello there


    Thanks in advance for any suggestions


    Tuesday, April 2, 2013 8:51 AM
  • We don't write scripts here we help you write them.

    See for /?

    for /f "tokens=1 delims=. " %A in ('echo %time%') do set time1=%A


    David Candy

    Tuesday, April 2, 2013 9:01 AM
  • I find another approach to display time, but don't know what -11 represents on following case, 8 represents the positions.

    Do you have any suggestions? 

    Thanks you very much for suggestions

    echo %time:~-11,8% %date% Hello world>C:\tempfile.txt
    copy /y C:\tempfile.txt+C:\log.txt C:\tempfile.txt
    copy /y C:\tempfile.txt C:\log.txt
    Del C:\tempfile.txt


    Thanks in advance for any suggestions





    Tuesday, April 2, 2013 9:53 AM
  • Type set /? for an explanation.


    David Candy

    Tuesday, April 2, 2013 10:01 AM
  • Thanks you very much for suggestions


    Thanks in advance for any suggestions

    Tuesday, April 2, 2013 10:30 AM