How to use the date command for DOS batch processing _dos/bat

Source: Internet
Author: User
Tags echo command
1. System Help
C:\>date/?
Displays or sets the date.

Date [/T | date]

Just type Date without arguments, you can display the current date setting, and prompt
You enter a new date. Press ENTER to keep the original date.

If the command extension is enabled, the DATE command supports/T command options; the command option tells
The command prints only the current date, but does not prompt for a new date.

2.data/t Parameter Description

This parameter outputs the current date, for example:

c:\>date/t
2002-10-28 Monday

This date format may be different from the date format in the environment variable, such as

C:\>echo%date%
Monday 2002-10-28

With the for command, you can combine different expressions, such as:

c:\>for/f "tokens=1,2"%a in (' date/t ') do set date=%a

C:\>set date=2002-10-28
What this statement does is to remove the part that contains no weeks in the date/t.

We can draw the same result with simpler commands, such as:

C:\>echo%date%
Monday 2002-10-28

C:\>echo%date:~4,10%
2002-10-28

If you want to create a date folder or file name in a batch, you can simply do it, such as

C:\>mkdir%date:~4,10%
C:\>dir 2*
The volume in drive C is a local disk
The serial number of the volume is 402a-3a7e

C:\ The directory

2002-10-28 19:34 <DIR> 2002-10-28
0 Files 0 bytes
1 Directory 79,282,176 Free bytes

C:\>copy MyList mylist%date:~4,10%
1 files have been copied.

C:\>dir my*
The volume in drive C is a local disk
The serial number of the volume is 402a-3a7e

C:\ The directory

2002-09-15 10:00 MyList
2002-09-15 10:00 mylist2002-10-28
3 Files 446 bytes
0 Directory 79,282,176 Free bytes

This is useful for some automated operations in Oracle.

As you can see, with the combination of simple DOS commands, we can do a lot of seemingly complex things, just our thinking

Not always able to get there.

3. About the format of%date:~4,10%

The meaning of this command is to take the 4-10 middle character in the output of date.
For different machines, there may be different time formats, which can be set in the Control Panel.
You can view the current settings by using the Echo command, such as:

C:\>echo%date%
Monday 2002-10-28

Of course, we can also take the value of the week:

C:\>echo%date:~0,3%
Monday

Of course, you can also come from different directions:

C:\>echo%date:~-10%
2002-10-28

C:\>echo%date:~0,-10%
Monday

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.