其他资源:

其他资源:

这是批处理文件的一部分(在 Win 7 和 10 中运行良好,但在 Win 11 中不行)。之前,批处理文件创建了一个名为“Today”的文件夹,并将文件复制到其中。这应该将文件夹“Today”更改为日期,格式为 YYYY-MM-DD:

setlocal enableextensions 
set name=%date:~-4,4%-%date:~-10,2%-%date:~7,2%
Rename "Today" "%name%"

事情是这样的:

D:\>set name=5/23-e -05
D:\>Rename "today" "5/23-e -05"
The system cannot find the path specified.

答案1

@echo off && cd /d D:\

for /f delims^=. %%i in ('
     wmic os get localdatetime ^| findstr [0-9]
   ')do set "_date=%%~i"
    
Set "_name=%_date:~0,4%-%_date:~4,2%-%_date:~6,2%"

Rename "Today" "%_name%"

%date%变量将始终按照语言、国家/地区和/或用户自定义/配置的格式显示日期,也就是说,它不遵循固定的布局/模式,我们可以以预测的方式工作,从而依靠固定/可预测的布局来操纵输出字符串

The default output from DATE /T in the United States is "1/23/1997" but this is dependent on the country/locale.
The default short date and time formats for a selection of countries are as follows:

 Country/language  CountryCode Locale Date_format  Time_format

   Argentina-Spanish     054   2C0A   23/01/1997   9:45:30 am
   Australia             061   0C09   23/01/1997   9:45:30 AM
   Austria               043   0C07   23.01.1997   09:45:30
   Brazil-Portuguese     055   0416   23/01/1997   09:45:30
   Belgium-French        032   080C   23/01/1997   09:45:30
   Belgium-Dutch         032   0813   23/01/1997   9:45:30
   Canada-French         002   0C0C   1997-01-23   09:45:30
   Canada-English        002   1009   01/23/1997   9:45:30 AM
   China/PRC-Chinese     086   0804   1997/1/23    9:45:30
   China/HK-Chinese Trad 852   0C04   23/1/1997    9:45:30
   Czech Republic        042   0405   23.1.1997    9:45:30 
   Denmark               045   0406   23-01-1997   09.45.30
   Finland               358   040B   23.1.1997    9:45:00
   France                033   040C   23/01/1997   09:45:30
   Germany               049   0407   23.01.1997   09:45:30
   Greece                030   0408   23/01/1997   9:45:30 μμ
   Hungary               036   040E   1997.01.23.  9:45:30 
   India-Hindi           091   0439   23-01-1997   09:45:30
   India-Bengali         091   0445   23-01-97     09.35.00 
   India-Telugu          091   044A   23-01-97     09:45:30 
   Italy                 039   0410   23/01/1997   09.45.30 
   Japan                 081   0411   1997/01/23   9:45:30
   Netherlands           031   0413   23-01-1997   9:45:30 
   Norway                047   0414   23.01.1997   09:45:30 
   Poland                048   0415   1997-01-23   09:45:30 
   Portugal              351   0816   23-01-1997   09:45:30 
   Spain                 034   0C0A   23/01/1997   9:45:30 
   Sweden                046   041D   1997-01-23   09.45.30
   Switzerland-French    041   100C   23.01.1997   09:45:30
   Switzerland-German    041   0807   23.01.1997   09:45:30
   Switzerland-Italian   041   0810   23.01.1997   09:45:30
   United Kingdom        044   0809   23/01/1997   09:45:30
   United States         001   0409   1/23/1997    9:45:30 AM

获得预测/可预测输出的另一种方法是在 for 循环过滤中使用 wmic,因为它wmi总是返回相同的字符串,而不管国家、自定义、用户设置、Windows 版本等......

Wmic OS Get localdatetime /value返回字符串:

LocalDateTime=20230906105958.182000-180

答案For /F loop ( wmic | filter Regex )do..是:

for /f delims^=. %%i in ('wmic os get localdatetime ^| findstr [0-9]')do set "_date=%~i"

set "_date=20230906105958"

使用字符串操作 # 根据位置提取子字符串

for /f delims^=. %%i in ('wmic os get localdatetime ^| findstr [0-9]')do set "_date=%%~i"
echo/%_date:~0,4%-%_date:~4,2%-%_date:~6,2%

结果:

2023-09-06

花些时间获取与您的问题和答案相关的参考详细信息:



其他资源:

相关内容