windows 如何在批处理 (.bat) 文件中创建名称为当前日期的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5485853/
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
How to create a folder with name as current date in batch (.bat) files
提问by Ramesh Soni
I don't know much about windows .bat file syntax. My simple requirement is to create a folder at a specific location with name as current date. I tried searching this on google but didn't get any good option. Is there any way to do this?
我不太了解 windows .bat 文件语法。我的简单要求是在特定位置创建一个文件夹,名称为当前日期。我试着在谷歌上搜索这个,但没有得到任何好的选择。有没有办法做到这一点?
采纳答案by Franck Freiburger
Try this (an equivalent of bash backquotes):
试试这个(相当于 bash 反引号):
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir %datestr%
For further information, see http://ss64.com/nt/for_cmd.html
有关更多信息,请参阅http://ss64.com/nt/for_cmd.html
回答by Stefano Travelli
mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
回答by Joey
Quick and dirty: If you can live with the date being UTC instead of local, you can use:
快速而肮脏:如果您可以接受 UTC 而不是本地日期,则可以使用:
for /f "skip=1" %%d in ('wmic os get localdatetime') do if not defined mydate set mydate=%%d
md %mydate:~0,8%
Works in all locales. Only on XP and higher, though.
适用于所有语言环境。不过,仅适用于 XP 及更高版本。
回答by Simon G.
You need to get rid of the '/' characters in the date before you can use it in mkdir like this:
您需要去掉日期中的 '/' 字符,然后才能像这样在 mkdir 中使用它:
setlocal enableextensions
set name=%DATE:/=_%
mkdir %name%
回答by jelde015
If you want mm-dd-yyyy
format you can use:
如果你想要mm-dd-yyyy
格式,你可以使用:
mkdir %date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%
回答by nirmalraj17
This depends on the regional settings of the computer, so first check the output of the date using the command prompt or by doing an echo of date.
这取决于计算机的区域设置,因此首先使用命令提示符或通过执行日期回显来检查日期的输出。
To do so, create a batch file and add the below content
为此,创建一个批处理文件并添加以下内容
echo %date%
pause
It produces an output, in my case it shows Fri 05/06/2015.
它产生一个输出,在我的例子中它显示周五 05/06/2015。
Now we need to get rid of the slash (/)
现在我们需要去掉斜线 (/)
For that include the below code in the batch file.
为此,在批处理文件中包含以下代码。
set temp=%DATE:/=%
if you echo the "temp", you can see the date without the slash in it.
如果你回显“temp”,你可以看到没有斜线的日期。
Now all you need to do is formatting the date in the way you want.
现在您需要做的就是按照您想要的方式格式化日期。
For example I need the date in the format of YYYYMMDD, then I need to set the dirname as below
例如我需要YYYYMMDD格式的日期,然后我需要设置目录名如下
To explain how this works, we need to compare the value of temp
为了解释这是如何工作的,我们需要比较 temp 的值
Fri 05062015.
周五 05062015。
now position each characters with numbers starting with 0.
现在用从 0 开始的数字定位每个字符。
Fri 0506201 5
01234567891011
周五 0506201 5
01234567891011
So for the date format which I need is 20150605,
所以对于我需要的日期格式是 20150605,
The Year 2015, in which 2 is in the 8th position, so from 8th position till 4 places, it will make 2015.
2015年,其中2位在第8位,所以从第8位到第4位,就是2015年了。
The month 06, in which 0 is in the 6th position, so from 6th position till 2 places, it will make 06.
06 月,其中 0 位于第 6 位,因此从第 6 位到第 2 位,则为 06。
The day 05, in which 0 is in the 4th position, so from 4th position till 2 places, it will make 05.
05 日,其中 0 位于第 4 位,因此从第 4 位到第 2 位,将成为 05。
So finally to set up the final format, we have the below.
所以最后要设置最终格式,我们有以下内容。
SET dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"
To enhance this date format with "-" or "_" in between the date, month and year , you can modify with below
要在日期、月份和年份之间使用“-”或“_”增强此日期格式,您可以使用以下内容进行修改
SET dirname="%temp:~8,4%-%temp:~6,2%-%temp:~4,2%"
or
或者
SET dirname="%temp:~8,4%_%temp:~6,2%_%temp:~4,2%"
So the final batch code will be
所以最终的批处理代码将是
======================================================
================================================== ====
@echo off
set temp=%DATE:/=%
set dirname="%temp:~8,4%%temp:~6,2%%temp:~4,2%"
mkdir %dirname%
======================================================
================================================== ====
The directory will be created at the place where this batch executes.
该目录将在此批处理执行的位置创建。
回答by Simon G.
echo var D = new Date() > tmp.js
echo D = (D.getFullYear()*100+D.getMonth()+1)*100+D.getDate() >> tmp.js
echo WScript.Echo( 'set YYYYMMDD='+D ) >> tmp.js
echo @echo off > tmp.bat
cscript //nologo tmp.js >> tmp.bat
call tmp.bat
mkdir %YYYYMMDD%
回答by Mark snow
for /F “tokens=1-4 delims=/ ” %%A in (‘date /t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)
set CurrentDate=%DateDay%-%DateMonth%-%DateYear%
md %CurrentDate%
This will give you a newly created folder with today's date, in the format of DD-MM-YY
这将为您提供一个新创建的带有今天日期的文件夹,格式为 DD-MM-YY
Sourced from: Ali's Knowledge Base
来源:阿里知识库
回答by user2138753
I had a problem with this because my server ABSOLUTELY had to have its date in MM/dd/yyyy format, while I wanted the directory to be in YYYY-MM-DD format for neatness sake. Here's how to get it in YYYY-MM-DD format, no matter what your regional settings are set as.
我遇到了这个问题,因为我的服务器绝对必须将其日期设为 MM/dd/yyyy 格式,而为了整洁起见,我希望目录采用 YYYY-MM-DD 格式。无论您的区域设置如何,以下都是以 YYYY-MM-DD 格式获取它的方法。
Find out what gets displayed when you use %DATE%:
找出使用 %DATE% 时显示的内容:
From a command prompt type:
从命令提示符键入:
ECHO %DATE%
Mine came out 03/06/2013 (as in 6th March 2013)
我的是 03/06/2013(截至 2013 年 3 月 6 日)
Therefore, to get a directory name as 2013-03-06, code this into your batch file:
因此,要获得 2013-03-06 的目录名称,请将其编码到您的批处理文件中:
SET dirname="%date:~6,4%-%date:~0,2%-%date:~3,2%"
mkdir %dirname%
回答by Blorgbeard is out
This should work:
这应该有效:
mkdir %date%
If it doesn't, try this:
如果没有,请尝试以下操作:
setlocal enableextensions
mkdir %date%