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

How to format date string under DOS batch processing

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to format the date string under DOS batch processing, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

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 www.yisu.com

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:

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 www.yisu.com

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:

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):

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

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.

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.

The above is all the contents of the article "how to format date strings in DOS batches". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 214

*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