Our forums are currently in maintenance mode and the ability to post is disabled. We will be back up and running as soon as possible. Thanks for your patience!



DOS command HELP!!!

By danday3953 ·
I am writing a short command script and I need to have the directory in which the date is stored labeled as the date. Every time I think I have a new way to do it, it flops. Tried:
echo %date% >> date.txt | mkdir << C:\date.txt and: SET DATE = %date% then mkdir C:\DATE and: a variation of TYPE, SET, and MKDIR commands. Can someone help quickly, or am I doomed to the RIF???


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Go here for your DOS commands...


DATE mm-dd-yy

Displays and/or sets the system date.

Please post back if you have any more problems or questions.

Collapse -

Using Date and Time in DOS batch files.

by 1bn0 In reply to DOS command HELP!!!

Copy of my notes stored in my yahoo notepad.

Using the current date in a batch file

The date command displays the current date and gives the operator the opportunity to change the current date.

Tue 06/03/2008
Enter the new date: (mm-dd-yy)

The %date:~% variable returns the current date as a string

C>echo %date:~%
Tue 06/03/2008

The following syntax can be used to extract desired text from the string returned by %date:~%


% start replacement variable
date:~ get the current date
-x move the cursor (-)X number of characters,0 is apparently the first character, or move back from the end using - numbers.
> C>echo %date:~4,2%%date:~7,2%%date:~12,2%
> and
> C>echo %date:~-10,2%%date:~-7,2%%date:~-2,2%
> both return the same result
> 060308
> C:>
> from the date string "Tue 06/03/2008"

, seperator
y display next Y characters from the cursor point
% end replacement variable

Date string Output Extracted from:
%date:~-4,4% 2008 Tue 06/03/2008
%date:~-7,2% 03 Tue 06/03/2008
%date:~-0,2% Tu Tue 06/03/2008
%date:~-0,3% Tue Tue 06/03/2008
%date:~-10,4% 06/0 Tue 06/03/2008 (Note: the / counts as one of he four characters)

%date:~-10,2% 06 Tue 06/03/2008
Date string variables can be combine to pull together the various parts YYY MM DD

%date:~-4,4%%date:~-7,2%%date:~-10,2% 20080306

Tue 06/03/2008Tue 06/03/2008Tue 06/03/2008
^xxxx ^xx ^xx

Using the current time in a batch file

The time command displays the current time and gives the operator the opportunity to change the current date.

The current time is: 9:24:52.64
Enter the new time:

The %time:~% variable returns the current time as a string

C>echo %time:~%


Note that windows returns the time to one-one hundreths of a second. ( at least on this machine it does) and there is a space preceding the hour digit,9.

A little bit of extra work is required to ensure hours between 0 and 9 are handled correctly.

Reference information obtained from:

"this routine works before and after ten in the morning. Silly DOS, it doesn't put a leading zero in unless you tell it to. Here's how to tell DOS to put in the leading zero if the current time is prior to 10:00am."

REM Create sub directory called \yymmdd_hhmmss
REM where yymmdd_hhmmss is a date_time stamp like 030902_134200

set hh=%time:~0,2%

REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.

if "%time:~0,1%"==" " set hh=0%hh:~1,1%

set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%

md h:\%yymmdd_hhmmss%

In English:

Times from midnight, 0:00 a.m. to 9:59 a.m. will return a single digit preceded by a space. It is usually desired to replace the space with a leading zero (0).

To accomplish this an environment variable to correctly reflect the hour in two digits is created using the %time:~% variable.

"if "%time:~0,1%"==" " set hh=0%hh:~1,1%"
if the left most digit is empty then set the environment variable hh to 0 + second digit from the begining.

%time:~% returns " 9:30:08.69"

set hh=09 0 + " 9:30:08.69"

then use %hh% as a variable in your command string to create a second variable with the complete date & time format yuo wish using %hh% to give you the hour in 2 digits.

"set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%"

simple example , if the extra space in place of the 10's digit in the hour is not a problem:

c>echo The current hour is %time:~-11,2% the current minute is %time:~-8,2% and the current hundreth seconds are %time:~-2,2%

The current hour is 9 the current minute is 55 and the current hundreth seconds
are 26

Collapse -

Kick *** post

by The Altruist In reply to Using Date and Time in DO ...

I was about to suggest something in VBScript, but that one just blows mine away.

Rock on.

Collapse -

Works ok in vbscript as well.

by 1bn0 In reply to Kick ass post

I researched it when I was repeatedly running a script that retrieves the logon script assigned to each user in the active directory.

I used it to add the date onto the filename so I could always identify the most recent version.

I was changing 300 users from logon batch files to one integrated logon.vbs script.

Related Discussions

Related Forums