Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of formatting date strings under DOS batch processing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the methods of formatting date strings under DOS batch processing. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

When it comes to the output of date strings, it's really annoying.

Under Linux, you can get the output you want with a simple sentence: date'+% Y% m% dmurt% H% M% S': 20120331-064219

But under windows, it takes some work to get this.

1. Format the output directly with a string

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

C:\ > echo% date%-%time%

2012-03-31 Saturday-6 purl 44purl 02.50

So, to get the output 20120331-64402, you can intercept the string like this:

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

20120331-65406

It means to take 4 characters from the 0 position, and so on. This method cannot truncate spaces. Be annoyed

two。 It seems better to break and intercept with a for statement.

Let's take a look at the use of for:

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 (only one) / / the line that begins with what character to ignore

Skip=n-refers to the number of lines ignored at the beginning of the file.

Delims=xxx-refers to the delimiter set. This / / specified split character that replaces spaces and tabs is included in delims=;:. Used ";", ":", "." Make a separation

Default delimiter set.

Tokens=x,y,m-n-refers to which symbol of each line is passed to each iteration

The for itself. This results in the allocation of additional variable names. Mmurn

The format is a range. Specify the mth through the nth symbol. If

The last character in a symbol string, an asterisk.

Then the additional variables will be parsed after the last symbol

Assign and accept reserved text for lines.

Usebackq-specifies that the new syntax has been used in the following class cases:

Execute a string in quotation marks as a command and a single

The quotation mark character is a text string command and is allowed in filenameset

Use double quotation marks to expand the file name in.

Referring to this usage, we can format the output of the date string:

Because the date result is: 2012-03-31 Saturday

Because the delimiter is'- 'and space'', take 3 paragraphs, such as:

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

20120331

Look at the time again:

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 like this:

The code is as follows:

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

C:\ > 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 it in batches, and it looks 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 more flexible.

3. Another option 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 results are as follows, which is quite interesting.

The code is as follows:

C:\ > cscript / nologo c:\ shared\ datescript.vbs

20120331-0711

Of course, this is still not ideal. You can write a batch and put the result into 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 third method is the most complex, and it is not clear whether it will work correctly on all windows platforms, but the result should be fixed. Does not change with the change of date,time format. But 1 and 2 depend on the format of the date and time. That's all.

It's still more fixed under linux.

About "what are the methods of formatting date strings in DOS batches?" this article ends here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report