Create a Date and Time Stamp in Batch Files

Ask a question
A batch file is a text file which contains a series of commands executed by a command interpreter for Microsoft's Windows operating system. It batches a set of commands into a single file with a .bat filename extension.

Windows versions of operating systems run from DOS and use the same command interpreter to execute batch files. You can use date and time DOS commands to generate timestamps in batch files. You can also schedule the batch files to run at a particular time and automate series of commands. You can use the DIR command which allows you to see the available files and subdirectories in the directory.

Create a Batch File with Filenames and Timestamps

Here is a batch file that will display all files, subfolders and subfolder files with both date created and date modified timestamps. The resulting file may be saved as either a .txt or .xls file.


DIR [drive:][path][filename] [/A[[:]attributes]] /B /C /D /L /N   
[/O[[:]sortorder]] /P /Q /S [/T[[:]timefield]] /W /X /4

[drive:][path][filename]

N.B. This list specifies drive, directory, and/or files to list

/A- Displays files with specified attributes.
attributes D Directories R Read-only files
H - Hidden files A Files ready for archiving
S - System files - Prefix meaning not
/B - Uses bare format (no heading information or summary).
/C - Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D - Same as wide but files are list sorted by column.
/L - Uses lowercase.
/N - New long list format where filenames are on the far right.
/O - List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E - By extension (alphabetic) D By date/time (oldest first)
G - Group directories first - Prefix to reverse order
/P - Pauses after each screenful of information.
/Q - Display the owner of the file.
/S - Displays files in specified directory and all subdirectories.
/T - Controls which time field displayed or used for sorting
timefield C Creation
A - Last Access
W - Last Written
/W - Uses wide list format.
/X - This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name inserted
before the long name. If no short name is present, blanks are
displayed in its place.
/4 - Displays four-digit years


Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen) — for example, /-W.

============================================================ ======

dir "D:\Documents and Settings\dave\Desktop\"/T:C /T:W /S /Q >> "d:\Documents and   

Settings\dave\Desktop\dir.txt"

This will produce the desired result. (You need to put the switches after the source details.)
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team