Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm writing a batch file that will run overnight on NT 4.0. I am redirecting the output from that file to a log.txt file. I would like to have the batch file write the current date to the screen (and thus be redirected to my log file). Hopefully there is a simple solution that requires no user interaction. I have tried the date command, but it prompts the user for the new date value, halting the script, and defeating the whole purpose of automating this task.
Please respond if you can help.
Thanks,
Doug

If Command Extensions are enabled the DATE command supports the /T switch which tells the command to just output the current date, without prompting for a new date. Hope this works for you.

Q. How can I append the date and time to a file?
A. You can use the batch file below which will rename a file to filename_YYYYMMDDHHMM.
@Echo OFF
TITLE DateName
REM DateName.CMD
REM takes a filename as %1 and renames as %1_YYMMDDHHMM
REM
REM -------------------------------------------------------------
IF %1.==. GoTo USAGE
Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMPDATE /T > %CURRDATE%
TIME /T > %CURRTIME%Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%jSet PARSEARG="eol=; tokens=1,2,3* delims=:, "
For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%kEcho RENAME %1 %1_%YYYYMMDD%%HHMM%
RENAME %1 %1_%YYYYMMDD%%HHMM%
GoTo END:USAGE
Echo Usage: DateName filename
Echo Renames filename to filename_YYYYMMDDHHMM
GoTo END:END
REM
TITLE Command PromptExample:
D:\Exchange> datetype logfile.log
RENAME logfile.log logfile.log_199809281630Other date options include LOGTIME.exe which enables you to specify a string and then writes the time followed by the string to the file logtime.log at the current default directory.
The other option is NOW.exe which just replaces itself with the date and time, e.g.
D:\temp>now Batch complete
Mon Sep 28 15:54:19 1998 -- Batch completeBoth of the above utilities are part of the resource kit.
Another way is by using the following FOR command, a log file can be created using real dates.
rem created unique log filename, e.g. Wed0804
FOR /F "tokens=1-4 delims=/" %%i in ('date/t') do set file=%%i%%j%%k
Set LOG=drive:\directory\filename-%file%.logThe result is a file named filename-date.log.
But how do you redirect the output from a batch-file into a logfile ??

Thanks for the help, that works fine.
To answer the last question, I redirect to a log file by simply using the > symbol like this.
c:\>batchfile > log.txt
Any text that batchfile would normally print to the screen goes into log.txt instead. If you do that with one > symbol, it overwrites log.txt. If you do that with two >> symbols, it appends to log.txt. Both ways will create the file if it doesn't exist.
Since I'm doing this through the NT Scheduler, I just appended the redirection to the command that NT has scheduled in the properties of that scheduled task.
Doug

A simple and basic DOS command for this:
echo.|date >filename.txt
Note: there's a period after the "echo"; there are no spaces around that period.

create three batch files
create three batchfiles
batchfile 1
rem batchfile 1 exe.
rem this is the one you run
echo.| date| find "Current" > holddate.bat
call "holddate.bat"
current
<< this is the batchfiles name
rem batch "current.bat"
rename =recovend.txt %4%.txt
holddate.bat<< this is the third batchfileCurrent date is Wed 02-23-2000

![]() |
DOS Version
|
Imaging Turbo Pascal 7.0 ...
|

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