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 several 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?
This question is related to
text
batch-file
text-files
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.
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
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.
Just set the time and date to variables if it will be something that will be in a loop then
:top
set T=%time%
set D=%Date%
echo %T%>>log.txt
echo %d%>>log.txt
echo time:%T%
echo date:%D%
pause
goto top
I suggest making it nice and clean by putting:
@echo off
in front of every thing it get rid of the rubbish C:/users/example/...
and putting
cls
after the :top
to clear the screen before it add the new date and time to the display
A handy timestamp format:
%date:~3,2%/%date:~0,2%/%date:~6,2%-%time:~0,8%
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
Use the tail.exe from the Windows 2003 Resource Kit
@echo off
set log=%time% %date%
echo %log%
That is a batch for saving the date and time as a temporary variable, and displaying it. In a hurry, I don't have time to write a script to open a txt, maybe later.
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
Source: Stackoverflow.com