本文介绍了如何在Windows批处理文件中以"yyyymmdd"获取日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Windows批处理文件,并且batch file应该在特定文件夹中输出文件.我希望文件名编写如下:

filename-yyyymmdd.csv

其中yyyymmdd代表当前日期.

我的batch file代码如下:

cd:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn
bcp "SELECT TOP 100 * FROM xxxx.dbo.xxxxx" queryout c:\test\csv\confirmed.csv -t, -c -S xxxx\xxxxx -U xxx -P xxxxxxxxxx
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo %mydate%
copy c:\test\csv\confirmed.csv c:\test\csvwithdates\confirmed-%mydate%.csv

我得到以下文件名作为输出:confirmed-ay18.csv

所需的输出:confirmed-20180528.csv

我查看了有关StackOverflow的以下问题,但我很难实施建议的答案:

在Windows批处理文件中以YYYYMMDD格式获取日期

Windows批处理:将日期格式化为变量

我在做什么错了?

解决方案

动态环境变量DATE以取决于区域的格式(即根据Windows区域和使用的帐户的语言"选项中设置的国家/地区)提供当前本地日期. Wikipedia文章国家/地区的日期格式列出了世界各地国家/地区使用的各种日期格式./p>

因此有必要在命令提示符窗口echo %DATE%中使用运行批处理文件时使用的帐户运行,并查看输出日期字符串.

输出日期是从缩写的工作日开始还是输出的日期没有工作日?输出的第一天和下个月是数字还是输出的第一月和第二天?月输出为数字还是名称?是一个月中的某天或某个月的值小于10且输出的前导0,因此始终是两位数还是只有一位?

一个非常常见的命令行,用于使用./yyyyMMdd的任何其他定界字符将格式为ddd, dd.MM.yyyy的区域相关日期字符串修改为:

set "LocalDate=%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%"

从右边开始替换日期字符串,以使替换字符串与日期字符串开头是否存在工作日无关.

对于与地区相关的日期字符串ddd, MM/dd/yyyy,要使用的命令行为:

set "LocalDate=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%"

通过使用命令 ECHO 而不是 SET :

,可以在命令提示符窗口中运行命令,从而轻松找出需要替换的字符串:

echo %DATE%
echo %DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%
echo %DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%

另请参阅%date是什么:〜-4,4 %% date:〜-10,2 %% date:〜-7 ,2%_%time:〜0.2 %% time:〜3.2%平均值?

使用动态环境变量DATE的优点是速度,因为Windows命令处理器可以非常快速地完成此字符串替换.缺点是日期格式因地区/国家/地区的依赖性而有所不同.

与区域无关的解决方案正在使用命令 WMIC ,如下所示:

for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I"
set "LocalDate=%LocalDate:~0,8%"

WMIC 始终以 yyyyMMddHHmmss.microsecond±UTC偏移量(分钟)格式输出日期字符串,而与配置哪个国家/地区以及为该国家/地区设置哪种日期格式无关.

使用 WMIC 的缺点是执行所需的时间,因为它需要一秒钟以上的时间来输出日期字符串UTF-16 Little Endian编码,然后由命令 FOR 处理>.

请参阅上的答案,为什么%date%在作为计划任务执行的批处理文件中产生不同的结果?以获得非常详细的解释关于这两个命令行如何在Windows命令处理器cmd.exe上执行的工作.

完整的批处理文件:

@echo off
cd /D "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn"
bcp.exe "SELECT TOP 100 * FROM xxxx.dbo.xxxxx" queryout C:\test\csv\confirmed.csv -t, -c -S xxxx\xxxxx -U xxx -P xxxxxxxxxx
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I"
set "LocalDate=%LocalDate:~0,8%"
copy /Y C:\test\csv\confirmed.csv C:\test\csvwithdates\confirmed-%LocalDate%.csv
set "LocalDate="

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • cd /?
  • copy /?
  • echo /?
  • for /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

I am writing a Windows Batch file and the batch file is supposed to output a file in a specific folder. I want the filename to be written as follows:

filename-yyyymmdd.csv

where yyyymmdd stands for the current date.

My batch file codes stand as follows:

cd:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn
bcp "SELECT TOP 100 * FROM xxxx.dbo.xxxxx" queryout c:\test\csv\confirmed.csv -t, -c -S xxxx\xxxxx -U xxx -P xxxxxxxxxx
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo %mydate%
copy c:\test\csv\confirmed.csv c:\test\csvwithdates\confirmed-%mydate%.csv

I am getting the following filename as output: confirmed-ay18.csv

Desired output: confirmed-20180528.csv

I had a look into the following questions on StackOverflow but I am having a hard time implementing the suggested answers:

Get Date in YYYYMMDD format in windows batch file

Windows batch: formatted date into variable

What am I doing wrong?

解决方案

The dynamic environment variable DATE supplies the current local date in region dependent format, i.e. according to the country set in Windows Region and Language options for used account. Wikipedia article Date format by country lists the various date formats used in countries all over the world.

So it is necessary to run with the account used on running the batch file in a command prompt window echo %DATE% and look on output date string.

Does output date start with an abbreviated weekday or is the date output without weekday? Is first output day and next month with digits or is first output month and next day? Is the month output as number or with name? Is day of month or the month with a value less than 10 output with a leading 0 and so always with two digits or with just one digit?

A very common command line for modifying a region dependent date string in the format ddd, dd.MM.yyyy with . or / or any other delimiter character to yyyyMMdd is:

set "LocalDate=%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%"

The date string is substituted from right to make string substitution independent on presence of weekday at beginning of date string.

For region dependent date string ddd, MM/dd/yyyy the command line to use is:

set "LocalDate=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%"

Which string substitution is necessary can be easily find out by running the commands in a command prompt window with using command ECHO instead of SET:

echo %DATE%
echo %DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%
echo %DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%

See also What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?

The advantage of using dynamic environment variable DATE is the speed as this string substitution is done by Windows command processor very fast. The disadvantage is the varying date format because of region/country dependency.

A region independent solution is using command WMIC as demonstrated below:

for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I"
set "LocalDate=%LocalDate:~0,8%"

WMIC outputs the date string always in the format yyyyMMddHHmmss.microsecond±UTC offset in minutes independent on which country is configured and which date format is set for the country.

The disadvantage of using WMIC is the time required for execution as it needs more than a second to output the date string UTF-16 Little Endian encoded processed next by command FOR.

See answer on Why does %date% produce a different result in batch file executed as scheduled task? for a very detailed explanation on how those two command lines work on execution by Windows command processor cmd.exe.

Complete batch file:

@echo off
cd /D "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn"
bcp.exe "SELECT TOP 100 * FROM xxxx.dbo.xxxxx" queryout C:\test\csv\confirmed.csv -t, -c -S xxxx\xxxxx -U xxx -P xxxxxxxxxx
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I"
set "LocalDate=%LocalDate:~0,8%"
copy /Y C:\test\csv\confirmed.csv C:\test\csvwithdates\confirmed-%LocalDate%.csv
set "LocalDate="

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • copy /?
  • echo /?
  • for /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

这篇关于如何在Windows批处理文件中以"yyyymmdd"获取日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:49