Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm making a messaging batch, and I would like to add a timestamp to the messages sent. I know how to make it show on-screen but how can I get the batch to echo it to a text? All it echoes is 'TIME /T'. Kind of basic I'm sure, but...
I'm using delayed expansion if that makes any difference.
Babaloomba

Use the %time% environment variable.
echo %time% >> path\filename
You can change the output to suit your format.

I tend to use time /t in a for loop, especially if the time is to be displayed, because it *usually* outputs 12hr with am/pm at the end. Generally I find this easier to read, of course this is just a preference and to each his own.
for /f "delims=" %%g in ('time /t') do set 12hrtm=%%g
echo %12hrtm%This may have different results depending on your settings.

None of this is working. All I need to know is what syntax to put 'time /t' in to make the batch echo the actual time instead of 'time /t'.
Babaloomba

echo %time%
=====================================
If at first you don't succeed, you're about average.M2

Sorry variables starting with numbers in batch file...
FOR /F "DELIMS=" %%G IN ('TIME /T') DO SET HRTIME=%%G
ECHO %HRTIME%I tested the command in the my last post on the command line, and forgot about variables starting with numbers not being allowed because of the conflict with arguments.

I guess I wasn't clear.
I need the "time /t" command to be written into a .txt file. Not the %time% command, but time /t.
I figured out how to do it by myself today by writing;
time /t >> blah.txt
...but,
I'd like it to be in the same line as;
echo (!user!): !message! >> blah.txt
I want it to be;
echo #time# (!user!): !message! >> blah.txt
I've tried;
echo time /t (!user!): !message! >> blah.txt
echo %time% (!user!): !message! >> blah.txt
echo %time% /t (!user!): !message! >> blah.txt
echo %time /t% (!user!): !message! >> blah.txt
echo !time /t! (!user!): !message! >> blah.txt
echo !time! /t (!user!): !message! >> blah.txt%time% works, but gives military time with milliseconds.
So...
Babaloomba

is this too simple? It does work! Hope it helps
:: thanks to IVO and Mech2 for assistance
@echo off
cls
set message=
set /p message=
echo %time:~0,8% (%username%): %message% > c:\temp_msg.txt
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%A in (c:\temp_msg.txt) do (
set line=%%A
echo.!line!>> c:\msg_history.log)
::rem the next line if you dont want the history to pop up, i enabled it just to show you it works.
start c:\msg_history.log
del c:\temp_msg.txt

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |