在实际的运维中,可能会遇到用bat批处理命令将命令的执行结果,日志的内容等输出到txt的情况;
并且此时需要用时间格式为:年-月-日,时-分-秒作为txt的文件名,便于查询;
下面的代码就是具体的实现方法:
主要是批处理命令截取字符串和合并字符串的具体实例
echo off
echo 采用年月日,时分秒作txt文件名
rem 开启必要设置
setlocal enabledelayedexpansion
rem 截取时分秒的字符--否则默认的冒号是非法的文件名字符
set currentTime=%time:~0,2%点%time:~3,2%分%time:~6,2%秒
rem 获取年月日
set currentDate=%date:~0,4%年%date:~5,2%月%date:~8,2%日
rem 合并字符
set txtName=!currentDate!!currentTime!
echo 显示时间 !currentTime! >>!txtName!-自定义字符.txt
echo 显示日期 !currentDate! >>!txtName!-自定义字符.txt
pause
使用效果截图
![](https://i-blog.csdnimg.cn/blog_migrate/7c991234e33e510effbd4f8707453fa2.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/569e1bd6cdc9c4575691dfde5498a600.jpeg)