在创建日志文件时包含 %date% 会导致“找不到路径错误”

在创建日志文件时包含 %date% 会导致“找不到路径错误”

我尝试根据今天的日期创建动态命名的日志文件。我使用了在网上找到的示例,但似乎都不适合我。

C:\Users\Amit>echo hello > %date%.txt
The system cannot find the path specified.

C:\Users\Amit>echo hello > %date%.txt
The system cannot find the path specified.

C:\Users\Amit>echo hello > %date%.dat
The system cannot find the path specified.

C:\Users\Amit>echo hello > %date%.dat
The system cannot find the path specified.



C:\Users\Amit>echo hello > "%date%.dat"
The system cannot find the path specified.

C:\Users\Amit>echo hello > test.txt    #this works



C:\Users\Amit>echo hello >  %date%.txt
The system cannot find the path specified.

C:\Users\Amit>echo "testfile" >> backup-%DATE%.txt
The system cannot find the path specified.

C:\Users\Amit>echo "testfile" > backup-%DATE%.txt
The system cannot find the path specified.

我也复制粘贴了很多。我用的是Windows 7的

我不知道我犯了什么错误。

谢谢。

答案1

正如评论所说,您可以通过提取获取日期部分:

::  0123456789    offset table
::  12/01/2018    example of %DATE%, dependent on locale/country
set YYYY=%DATE:~6,4%
set DD=%DATE:~0,2%
set MM=%DATE:~3,2%

echo hello > %DD%_%MM%_%YYYY%.txt

您收到的错误是由%DATE%输出中的斜线引起的。文件名中不允许出现这些斜线。

相关内容