Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

batch file - Time is set incorrectly after midnight

I use the following to get the current date/time in a more readable format:

set day=%date:~4,2%
set mth=%date:~7,2%
set yr=%date:~10,4%
set hur=%time:~0,2%
set min=%time:~3,2%
set bdate=[%day%-%mth%-%yr%]-[%hur%-%min%]

This works well and outputs something like: [02-06-2020]-[22-59]

However, after midnight the time format changes from HH:MM:SS:MS to H:MM:SS:MS. That messes up the format because it includes the colon : in the time because of the characters are shifted over by one. That results in a not useful date/time information in the log file created after midnight.

Is there any way to get the time always in the format with two digits for the hour?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is that you're using the %DATE% and %TIME% variable values, which contain strings which are not consistent across locales, PC's, or users.

The best advice is to retrieve the information using an alternative method, for example:

@For /F "Tokens=1-5Delims=/: " %%G In (
    '""%__AppDir__%Robocopy.exe" : . /NJH /L|"%__AppDir__%find.exe" " 123""'
)Do @Set "bdate=[%%I-%%H-%%G]-[%%J-%%K]"

The method involves generating an error message from the built-in utility. We do that by asking it to copy from a target directory named : in the root of the current drive, . An error is returned because Windows does not allow directory names containing the character :. That error message, regardless of locale, PC, or user, always outputs a line starting with the date and time strings in a known format, yyyy/MM/dd hh:mm:ss.

Examples:

2020/02/08 01:25:04 ERROR 123 (0x0000007B) Accessing Source Directory C::
The filename, directory name, or volume label syntax is incorrect.

2020/02/08 01:25:04 ОШИБКА 123 (0x0000007B) Доступ к исходной папке C::
‘Ё*в*?бЁз?б?*п ?иЁ??* ? Ё??*Ё д*??*, Ё??*Ё ?*??Ё Ё?Ё ??в?? в??*.

2020/02/08 01:25:04 ERREUR 123 (0x0000007B) Copie du fichier C::
La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.

2020/02/08 01:25:04 FEHLER 123 (0x0000007B) Zugriff auf Zielverzeichnis C::
Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datentr?gerbezeichnungist falsch.

We run our Robocopy command inside a in order that we can capture the required strings according to their known line position, format, and separator characters. We also need to account for the fact that the output consists of more than one line. To select the line, I have used the built-in utility, and chosen to match only lines containing the string ?123 as it cannot appear elsewhere in the output. Next I split the line up using the known separator characters as delimiters and choose how many delimiter separated token components to return. I choose a forward slash, /, which delimits the individual date components, a colon. :, which delimits the individual time components, and a space, ?, which separates date and time from the rest of the line and each other. For your user case you only wanted five token components:

    ↓  ↓  ↓  ↓  ↓    delims
2020/02/08 01:25:04
↑    ↑  ↑  ↑  ↑
 1    2  3  4  5     tokens
 %G  %H %I %J %K

The final thing I do is to put each of the components together in the required order and format, saving it as a string value within a variable named bdate. You should be able to use that value elsewhere in the script, as %bdate%, or !bdate! if delayed expansion is enabled.

To get more information of how to use a , please open a Command Prompt window and enter for /?.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...