gpt4 book ai didi

batch-file - 批处理 : syntax error on %time% with environment variables

转载 作者:行者123 更新时间:2023-12-01 03:40:29 24 4
gpt4 key购买 nike

我正在使用以下变量在德语 Win7 环境中运行批处理脚本:

%date%_%time:~0,2%-%time:~3,2%-%time:~6,2%

该脚本从 10:00 到 23:59 运行良好。在 0:00 和 9:59 之间,我收到语法错误(我想是因为时间在“:”之前只有一位数字)

有人可以帮我弄这个吗?

谢谢!

最佳答案

仍然取决于语言环境:

set "_datetime=%date%_%time:~0,2%-%time:~3,2%-%time:~6,2%"
set "_datetime=%_datetime: =0%"

对于语言环境 独立 解决方案(是的,那时没有 _-):
for /F "tokens=2 delims==" %%G in (
'wmic OS get LocalDateTime /value'
) do @for /F "tokens=*" %%x in ("%%G") do (
set "_datetime=%%~x"
)
set "_datetime=%_datetime:~0,14%"
goto :eof

这里 for循环是
  • %%G检索 LocalDateTime值(value);
  • %%x删除返回值中的结束回车符( wmic 行为:每个输出行以 0x0D0D0A 结尾,而不是常见的 0x0D0A )。

  • 参见 Dave Benham 的 WMIC and FOR /F : A fix for the trailing <CR> problem

    关于batch-file - 批处理 : syntax error on %time% with environment variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241105/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com