Three methods for formatting date strings in DOS batch processing

Source: Internet
Author: User

Three methods for formatting date strings in DOS batch processing

In Linux, a simple sentence is: date '+ % Y % m % d-% H % M % s' to get the output you want: 20120331-064219 but in windows, it still takes some effort to get this.

It is annoying to talk about date string output.

 

In Linux, a simple sentence is: date '+ % Y % m % d-% H % M % s' to get the output: 20120331-064219.

However, in windows, it still takes some effort to get this.

 

1. format the output directly with a string

 

For example, if the time output on your machine is like this:

C:> echo % date %-% time %

Saturday-6:44:02. 50

To output 20120331-64402, You can intercept the string as follows:

C:> echo % date :~ 0, 4% % date :~ 5, 2% % date :~ 8, 2%-% time :~ 0, 2% % time :~ 3,2% % time :~ 6, 2%

20120331-65406

It indicates that it starts from 0 and contains 4 characters. This method cannot intercept spaces. Annoying

 

2. it seems better to use the for statement to split and intercept.

 

Let's take a look at the usage of:

 

The Code is as follows:

FOR/F ["options"] % variable IN (file-set) DO command [command-parameters]

FOR/F ["options"] % variable IN ("string") DO command [command-parameters]

FOR/F ["options"] % variable IN ('command') DO command [command-parameters]

 

 

Optinos

Eol = c-refers to the end of a line comment character (just one) // ignore the line whose character starts

Skip = n-indicates the number of rows ignored at the beginning of the file.

Delims = xxx-refers to the delimiter set. This character replaces the space and the Skip key // specified to be separated into delims =;:. Use ";", ":", "." To separate

Default delimiter set.

Tokens = x, y, m-n-indicates which symbol of each row is passed to each iteration.

For itself. This leads to the allocation of additional variable names. M-n

Format is a range. Use the nth symbol to specify mth. If

The asterisks of the last character in the symbol string,

Then the extra variable will be resolved after the last symbol

Distribute and accept reserved text of rows.

Usebackq-specify that the new syntax is used in the following situations:

A string enclosed in quotation marks when executed as a command and a single

The quotation mark character is a text string command and can be used in filenameset

Use double quotation marks to expand the file name.

With reference to this usage, We Can format and output the date string:

Because the date result is: Saturday

Because the Delimiter is '-' and there are spaces ', take 3 segments, for example:

C:> for/f "tokens = 1-3 delims =-" % 1 in ("% date %") do @ echo % 1% 2% 3

20120331

Time:

C:> echo % time %

6:59:20. 38

C:> for/f "tokens = 1-3 delims =.:" % 1 in ("% time %") do @ echo % 1% 2% 3

65939

The combination of the two can be used as follows:

 

The Code is as follows:

C:> for/f "tokens = 1-3 delims =-" % 1 in ("% date %") do set ttt = % 1% 2% 3

C: & gt; set ttt = 20120331

C:> for/f "tokens = 1-3 delims =.:" % 1 in ("% time %") do set ttt = % ttt %-% 1% 2% 3

C:> set ttt = 20120331-70131

 

 

Write batch processing becomes like this (goodtime. bat ):

 

 

The Code is as follows:

For/f "tokens = 1-3 delims =-" % 1 in ("% date %") do set ttt = % 1% % 2% % 3

For/f "tokens = 1-3 delims =.:" % 1 in ("% time %") do set ttt = % ttt %-% 1% % 2% % 3

 

 

Echo goodtime = % ttt %

This method is flexible.

 

3. Another method is to use VBScript to customize the output.

 

 

The Code is as follows:

Wscript. echo year (date) & right ("0" & month (date), 2) & right ("0" & day (date), 2) & "-" & right ("0" & hour (time), 2) & right ("0" & minute (time), 2)

 

 

The result is as follows, which is quite interesting.

 

 

The Code is as follows:

C:> cscript/nologo c: shareddatescript. vbs

20120331-0711

Of course, this is still not ideal. You can write a batch to get the result to the environment variable:

Mydate2.bat, the content is as follows:

@ Echo off

Cscript/nologo datescript. vbs> tmp.txt

For/f "tokens = *" % 1 in (tmp.txt) do set goodtime = % 1

Echo goodtime = % goodtime %

 

 

To sum up, the 3rd methods are the most complex. Currently, it cannot be determined whether they can run correctly on all windows platforms, but the results should be fixed. The format does not change with the date and time formats. However, values 1 and 2 depend on the date and time format. That's all.

Or linux is relatively fixed.

 

 

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.