windows 批处理文件以根据当前日期和时间创建文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19704716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:02:12  来源:igfitidea点击:

Batch file to create a folder based on current date & time

windowsbatch-filecmd

提问by TheNineteenNineties

we have a simple batch file that creates a backup of a folder and appends the date & time to the end.

我们有一个简单的批处理文件,用于创建文件夹的备份并将日期和时间附加到末尾。

We use this incrementally and it outputs a folder such as "data 28-04-13".

我们增量地使用它,它输出一个文件夹,例如“data 28-04-13”。

I would like to add the time to the end of this, however my code outputs time as HH:MM, which is not valid for a folder name as it includes a colon (:).

我想在此末尾添加时间,但是我的代码将时间输出为 HH:MM,这对文件夹名称无效,因为它包含冒号 (:)。

Please could someone modify my code to remove the :, or replace it with a ".".

请有人修改我的代码以删除:,或将其替换为“.”。

Thank You

谢谢你

@echo off & for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)

@echo off & for /F "tokens=1-4 delims=/ " %%D in ('time/t') do (
set DateTime=%%D
)

set CurrentDate=%DateDay%-%DateMonth%-%DateYear%-%DateTime%

md "F:\MobilePC\data %CurrentDate"

Answered my own question

回答了我自己的问题

So, this was the easiest way for me:

所以,这对我来说是最简单的方法:

set CurrentDate=%DateDay%-%DateMonth%-%DateYear%-%time:~0,2%.%time:~3,2%

Which outputs "31-10-13-11.35"

输出“31-10-13-11.35”

回答by foxidrive

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

此代码的前四行将在 XP Pro 及更高版本中为您提供可靠的 YY DD MM YYYY HH Min Sec 变量。

The built in cmd date and time variables are user configurable and so are unreliable for any general batch file.

内置的 cmd 日期和时间变量是用户可配置的,因此对于任何通用批处理文件都不可靠。

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

回答by MC ND

rem replace : with .
set myTime=%time::=.%

rem remove cents of second
set myTime=%myTime:~0,-3%

回答by Nirav Dave

You can set current date and time by doing this. I am using this daily in my batch file.

您可以通过执行此操作设置当前日期和时间。我每天都在我的批处理文件中使用它。

%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%

ouput :

输出:

201509141639 ( 14th sept 2015 04:39 PM )

回答by Harikrishnan

This code will create a folder named with current date & time and copies the full content from "D:\Tally Data" to "08-10-2016 17 23" folder.

此代码将创建一个以当前日期和时间命名的文件夹,并将完整内容从“D:\Tally Data”复制到“08-10-2016 17 23”文件夹。

It requires a folder named "Tally Data" in your computer's D drive.(copy below code to a text document and save as a DOS batch file)

它需要在您的计算机的 D 盘中有一个名为“Tally Data”的文件夹。(将下面的代码复制到文本文档中并保存为 DOS 批处理文件)

for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a

set time=%TIME:~0,2%" "%TIME:~3,2%

mkdir C:\%date:/=%" "%time%\Backup

xcopy "D:\Tally Data" C:\%date:/=%" "%time%\Backup /E /S /Q /Y

回答by Code Monkey2

http://www.dostips.com/DtTipsStringManipulation.php

http://www.dostips.com/DtTipsStringManipulation.php

Run a search for "Replace a substring."

运行搜索“替换子字符串”。

回答by greg zakharov

 setlocal
   set "time=%time::=%"
   rem check that date has / delimeter if not replace it for valid
   md %date:/=%_%time:~0,-3%
 endlocal