Recently, I need to back up files regularly on the project, and use BAT files to automatically generate folder names, which are accurate to seconds:
The bat script time setting is as follows:
pushd E:\Test set T=%date:~0,10%-%time:~0,2%%time:~3,2%%time:~6,2% md %T% popd
The meaning of each operation with the following value of the time variable is as follows:
%time:~0,2% means that the pointer is offset from the left to the right to the right, and then the 2-bit characters are extracted from the position where the pointer is offset to. The result is the hour field value
%time:~3,2% means that the pointer is offset from left to right by 3 bits, and then extracts 2-bit characters from the offset, and the result is the minute field value
%time:~6,2% means that the pointer is offset by 6 bits from left to right, and then extracts 2-bit characters from the offset, and the result is the second field value
There is a problem when using %time:~0,2%%time:~3,2%%time:~6,2%, that is, if TIME is 08 points, the computer displays 08, but in fact, there will be a problem when using %time:~0,2%, grabbing, because actually the time space +8 is caught. In this way, when using md to create a folder, two folders will be created because there are spaces in T:
For example, the current time is 2019-08-23, 08:30:30;
T = 20190823- 83030
md %T% will create 2 folders: 20190823- and 83030.
Solution:
Define h individually the hour, then replace the space in h with 0, and then add h to T:
set h=% =0% -> The '=' here means 'replace'
pushd E:\Test set h=%time~0,2% set h=% =0% set T=%date:~0,10%-%h%%time:~3,2%%time:~6,2% md %T% popd
This is the end of this article about the solution to the problem of spaces in BAT to obtain time. For more related content on BAT time space problems, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!