views:

3053

answers:

9

Hello,

I'm scripting a big batch file.

It records the date to a log.txt file:

@echo off
echo %date%, %time% >> log.txt
echo Current date/time is %date%, %time%.
@pause
exit

It can record it several times, on serveral lines. Now what I want to do is that the batch file file shows the last recorded date/time from the log.txt file.

How???

+3  A: 
type log.txt

But that will give you the whole file. You could change it to:

echo %date%, %time% >> log.txt
echo %date%, %time% > log_last.txt
...
type log_last.txt

to get only the last one.

viraptor
could it just get from the log.txt file not the last line, but the line above that? i heared something about FINDSTR, but i don't want to FIND it.
YourComputerHelpZ
i understand what you are saying, but it should do it time after time after time... like 100x... as much as possible. I want to publish this and it will be very great.
YourComputerHelpZ
+3  A: 

Use the tail.exe from the Windows 2003 Resource Kit

SqlACID
how to use it???
YourComputerHelpZ
tail -1 log.txt
SqlACID
works, but... i got it to work now by myself, without any extra tools...
YourComputerHelpZ
+1  A: 

Try this: use Find to iterate through all lines with "Current date/time", and write each line to the same file:

for /f "usebackq delims==" %i in (`find "Current date" log.txt`) do (echo %i > log-time.txt)
type log-time.txt

Set delims= to a character not relevant in the date/time lines. Use %%i in batch files.

Explanation (update):

Find extracts all lines from log.txt containing the search string.

For /f loops through each line the command inside (...) generates.

As echo > log-time.txt (single > !) overwrites log-time.txt every time it's executed, only the last matching line remains in log-time.txt

devio
hmmm. could you explain in detail what it does?
YourComputerHelpZ
A: 

hmm.. just found the answer. it's easier then i thought. it just needs a bunch more stuff:

@echo off
if not exist log.txt GOTO :write
echo Date/Time last login:
type log.txt
del log.txt
:write
echo %date%, %time%. >> log.txt
@pause
exit

So it first reads the log.txt file and deletes it. After that it just get a new file (log.txt) with the date & time!

I hope this helps other people!

(the only prob is that the first time it does not work, but then just enter in random value at log.txt.) (This problem is solved and edited.)

YourComputerHelpZ
If this really solves your issue, then you should not be appending ( >> ) to the log file. But, it would have seemed, based on the original problem statement, that you wanted to keep a history, not delete it every time.
Sinan Ünür
this will only write current date/time to log.txt, but not retrieve anything from the original log file, as your original question asked for.
devio
it does in the first 2 lines: echo.... type...
YourComputerHelpZ
A: 

Here's a version that doesn't fail if log.txt is missing:


@echo off
  if not exist log.txt goto firstlogin
  echo Date/Time last login:
  type log.txt
  goto end

:firstlogin
  echo No last login found.

:end
  echo %date%, %time%. > log.txt
  pause
Anders Sandvig
+1  A: 

Ok I wonder when's the use but, here are two snipets you could use:

lastlog.cmd

@echo off
for /f "delims=" %%l in (log.txt) do set TimeStamp=%%l
echo %TimeStamp%

Change the "echo.." line, but the last log time is within %TimeStamp%. No temp files used, no clutter and reusable as it is in a variable.

On the other hand, if you need to know this WITHIN your code, and not from another batch, change your logging for:

set TimeStamp=%date%, %time%
echo %TimeStamp% >> log.txt

so that the variable %TimeStamp% is usable later when you need it.

Jay
A: 

a handy timestamp format %date:~3,2%/%date:~0,2%/%date:~6,2%-%time:~0,8%

Cyril
A: 

Here is a good date and time code:

@echo off
if %date:~4,2%==01 set month=January
if %date:~4,2%==02 set month=February
if %date:~4,2%==03 set month=March
if %date:~4,2%==04 set month=April
if %date:~4,2%==05 set month=May
if %date:~4,2%==06 set month=June
if %date:~4,2%==07 set month=July
if %date:~4,2%==08 set month=August
if %date:~4,2%==09 set month=September
if %date:~4,2%==10 set month=October
if %date:~4,2%==11 set month=November
if %date:~4,2%==12 set month=December

if %date:~0,3%==Mon set day=Monday
if %date:~0,3%==Tue set day=Tuesday
if %date:~0,3%==Wed set day=Wednesday
if %date:~0,3%==Thu set day=Thursday
if %date:~0,3%==Fri set day=Friday
if %date:~0,3%==Sat set day=Saturday
if %date:~0,3%==Sun set day=Sunday
echo.
echo The Date is %day%, %month% %date:~7,2%, %date:~10,4% the current time is: %time:~0,5%
pause

Outputs: The Date is Sunday, September 27, 2009 the current time is: 3:07

Does not work on 7. Get: 'The Date is , 0-, 009 the current time is: 12:00
YourComputerHelpZ
This could be because my windows is in another language?
YourComputerHelpZ
A: 

To list the last line from a log file (at C:/logfolder/log.txt for example), use the following biterscripting commands.

# Read the log file in
var str log ; cat "C:/logfolder/log.txt" > $log

Specify the full path (unless the file is in the current directory) and enclose the path or file name in double quotes.

# List the last line.
lex "l" $log

The letter in the double quotes is Lower Case Ell (l) . l for last. You can also use a number 1, 5, etc to get the 1st, 5th line, etc.

For documentation, http://www.biterscripting.com/helppages/lex.html .

Patrick

P M
? U said BATCH. Windows, Commandprompt. Not biterscripting???
YourComputerHelpZ