如何在批处理文件中按日期时间创建文件夹
How to create folder by Date time in batch file
我正在尝试使用以下代码在批处理文件中按日期时间创建文件夹
@echo off & for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set Date=%%C
set DateYear=%%D
)
@echo off & for /F "tokens=1-4 delims=/ " %%D in ('time/t') do (
set DateTime=%%D
)
set CurrentDate=%Date%-%DateMonth%-%DateYear%-0%time:~0,2%.%time:~3,2%.%time:~6,2%
mkdir %CurrentDate%
使用这个我得到文件夹名称 22-02-2021-010.01.37
但是如果时间在 1 到 9 小时之间,我的文件夹将显示为
22-02-2021-0 9.59.19
0 和 9 总有一个 space 而 1 到 9 hr 不显示为 01,02,03 hr
答案应该是:
22-02-2021-009.59.19
set "CurrentDate=%Date%-%DateMonth%-%DateYear%-0%time:~0,2%.%time:~3,2%.%time:~6,2%"
set "CurrentDate=%currentDate: =0%"
mkdir %CurrentDate%
用 0
代替 currentdate
中的
提示:使用 set "var1=data"
设置值 - 这避免了由尾随空格引起的问题。比较时,使用if "thing1" == "thing2" ...
可以避免thing1/2
.
中的空格引起的问题
最好和正确的方法是使用独立于区域 day/month
顺序的日期,您可以使用 "WMIC os GET LocalDateTime"
作为来源,因为它是 ISO 顺序:
@echo off
Title Get FileName With Date and Time
Call :GetFileNameWithDateTime MyCurrentDate
echo %MyCurrentDate%
MkDir %MyCurrentDate%
pause & exit
::----------------------------------------------------------------------------------
:GetFileNameWithDateTime <FileName>
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set "MyDate=%%x"
set "%1=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%-%MyDate:~8,2%-%MyDate:~10,2%"
Exit /B
::----------------------------------------------------------------------------------
如果您想要可以在任何现代系统上运行的东西,并且仍然在您的特定 format/order 中输出目录名称,而不管用户或区域设置如何,那么……
你可以在 batch-file:
中这样做
@For /F "Tokens=1-6 Delims=/: " %%G In ('%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^| %SystemRoot%\System32\find.exe " 123"') Do @MD "%%I-%%H-%%G-0%%J.%%K.%%L"
或直接来自 Windows command-line, cmd.exe
:
For /F "Tokens=1-6 Delims=/: " %G In ('%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^| %SystemRoot%\System32\find.exe " 123"') Do @MD %I-%H-%G-0%J.%K.%L
我正在尝试使用以下代码在批处理文件中按日期时间创建文件夹
@echo off & for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set Date=%%C
set DateYear=%%D
)
@echo off & for /F "tokens=1-4 delims=/ " %%D in ('time/t') do (
set DateTime=%%D
)
set CurrentDate=%Date%-%DateMonth%-%DateYear%-0%time:~0,2%.%time:~3,2%.%time:~6,2%
mkdir %CurrentDate%
使用这个我得到文件夹名称 22-02-2021-010.01.37
但是如果时间在 1 到 9 小时之间,我的文件夹将显示为
22-02-2021-0 9.59.19
0 和 9 总有一个 space 而 1 到 9 hr 不显示为 01,02,03 hr
答案应该是:
22-02-2021-009.59.19
set "CurrentDate=%Date%-%DateMonth%-%DateYear%-0%time:~0,2%.%time:~3,2%.%time:~6,2%"
set "CurrentDate=%currentDate: =0%"
mkdir %CurrentDate%
用 0
代替 currentdate
提示:使用 set "var1=data"
设置值 - 这避免了由尾随空格引起的问题。比较时,使用if "thing1" == "thing2" ...
可以避免thing1/2
.
最好和正确的方法是使用独立于区域 day/month
顺序的日期,您可以使用 "WMIC os GET LocalDateTime"
作为来源,因为它是 ISO 顺序:
@echo off
Title Get FileName With Date and Time
Call :GetFileNameWithDateTime MyCurrentDate
echo %MyCurrentDate%
MkDir %MyCurrentDate%
pause & exit
::----------------------------------------------------------------------------------
:GetFileNameWithDateTime <FileName>
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set "MyDate=%%x"
set "%1=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%-%MyDate:~8,2%-%MyDate:~10,2%"
Exit /B
::----------------------------------------------------------------------------------
如果您想要可以在任何现代系统上运行的东西,并且仍然在您的特定 format/order 中输出目录名称,而不管用户或区域设置如何,那么……
你可以在 batch-file:
中这样做@For /F "Tokens=1-6 Delims=/: " %%G In ('%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^| %SystemRoot%\System32\find.exe " 123"') Do @MD "%%I-%%H-%%G-0%%J.%%K.%%L"
或直接来自 Windows command-line, cmd.exe
:
For /F "Tokens=1-6 Delims=/: " %G In ('%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^| %SystemRoot%\System32\find.exe " 123"') Do @MD %I-%H-%G-0%J.%K.%L