Batch Files - Date

  • UserId: 1
  • Title: Batch Files - Date
  • Url: date
  • Summary: Batch Files - Date
  • Search:
  • DateCreated:
  • DateModified:
  • Published: 1
  • Pageable: 1

In addition to creating log files, in a .txt format - you may also wish to include the date in the filename. For example, I used to have to rename my log files and organise them into folders, after each had been created by a batch file output.

Recently I found several lines of code which formatted the date and included it in the filename, thus eliminating the need for me to rename the log files:

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set date=%%c-%%b-%%a)

To call the date, you simply put %date%!

So a batch file that outputs to a log file, using the date as part of the filename would look something like this:

@echo off

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set date=%%c-%%b-%%a)

echo Script Title. >> "C:\%date%_log.txt" echo Here is some text. >> "C:\%date%_log.txt" echo Here is some more text. >> "C:\%date%_log.txt" echo Executed on %date%. >> "C:\%date%_log.txt"

The snippet: >> "C:\%date%_log.txt" outputs the line to a text file, with the current date, along with _log.txt as the filename. For today, the log file would appear as:

2006-08-29_log.txt

Using this code, along with the other pieces that I have given you, should enable you to create batch files for backing up and defragging, along with mapping network drives - all of which can output to a .txt file for logging and using the date as a filename, for archiving purposes. Great eh?

Categories that this Post has been filed under

Tags that this Post has been tagged with

Comments

There are no Comments at this time.


Add a Comment