Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从批处理文件中的日期提取月份和年份_Batch File - Fatal编程技术网

Batch file 从批处理文件中的日期提取月份和年份

Batch file 从批处理文件中的日期提取月份和年份,batch-file,Batch File,我需要使用批处理文件将当前系统日期的月份和年份存储在两个不同的变量中。月份格式应为MMM,年份格式应为yyyy。今天的日期是2019年6月10日。所以月变量应该有六月,年变量应该有2019。 有人能帮我吗 谢谢。cmd必须获得月份名称,但如果您愿意(ab)使用Powershell: for /f "tokens=1,2 delims=_" %%a in ('powershell get-date -UFormat "%%Y_%%B"') do set "y=%%a" & set "m=%

我需要使用批处理文件将当前系统日期的月份和年份存储在两个不同的变量中。月份格式应为MMM,年份格式应为yyyy。今天的日期是2019年6月10日。所以月变量应该有六月,年变量应该有2019。 有人能帮我吗

谢谢。

cmd
必须获得月份名称,但如果您愿意(ab)使用Powershell:

for /f "tokens=1,2 delims=_" %%a in ('powershell get-date -UFormat "%%Y_%%B"') do set "y=%%a" & set "m=%%b"
echo Year is %y% and month is %m%

关于可用的格式,请咨询。

如果您愿意硬编码月份缩写(或名称)列表,那么使用纯批处理并不太困难

@echo off
setlocal enableDelayedExpansion

set "months= 1:Jan 2:Feb 3:Mar 4:Apr 5:May 6:Jun 7:Jul 8:Aug 9:Sep 10:Oct 11:Nov 12:Dec"

for /f "skip=1 tokens=1,2" %%X in (
  '"wmic path win32_localtime get month, year"'
) do if "%%Y" neq "" for /f %%M in ("!months:* %%X:=!") do (
  set "month=%%M"
  set "year=%%Y"
)

echo month=!month! year=!year!
months
值的前导空格很重要


由于WMIC产生UTF-16输出,因此需要使用
IF
语句,并且当由
FOR/F
解析时,转换为ANSI时存在一个缺陷,导致除了回车符(0x0D)之外没有其他内容的额外行。
IF
语句过滤掉不需要的“空”行。

该文件的任何示例?变量的命名方式是否有帮助?你试过代码吗?无论如何,我会用正则表达式切掉有希望的部分,然后尝试解析它们。只是想澄清:
MMM
是“短月名”(三个字母)
MM
是“两位数”,而
MMMM
是“全月名”。所以你实际上是在寻找
MMMM