Board logo

标题: [日期时间] 【已解决】批处理中如何取当天的日期并按指定样式格式化日期? [打印本页]

作者: ping192168101    时间: 2015-12-2 16:37     标题: 【已解决】批处理中如何取当天的日期并按指定样式格式化日期?


批处理作用:

有一个批处理脚本,用来收集windows系统日志,然后通过find 查找日志文本中的日期为12/1/2015的日志然后另存为一个文本文件,

set k3=%date:~5,2%/%date:~8,2%/%date:~0,4%
type 11.txt|find "%k3%" >c:\1111.txt

遇到的问题:

批处理取的日期格式为12/01/2015,请教如何取日期为12/1/2015


12/1/2015        22:56:54        4        7010        20010        Microsoft-Windows-UserPnp        NT AUTHORITY\SYSTEM               
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        Plug and Play 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        Remote Desktop Services 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        Windows Management Instrumentation 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        Superfetch 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-juWindows Defender 停止
12/1/2015        22:56:54        2        0        4001        Microsoft-Windows-WLAN-AutoConfig        NT AUTHORITY\SYSTEM               
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        WLAN AutoConfig 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-ju        Windows Font Cache Service 停止
12/1/2015        22:56:54        4        0        7036        Service Control Manager        N/A        zz-juo        Cryptographic Services 停止
12/1/2015        22:56:55        4        0        7036        Service Control Manager        N/A        zz-ju        Windows Search 停止
12/1/2015        22:56:58        4        103        109        Microsoft-Windows-Kernel-Power        N/A        zz-juo        6 0 0
12/1/2015        22:57:02        4        0        13        Microsoft-Windows-Kernel-General        N/A        zz-juo       
12/2/2015        7:40:04        4        0        12        Microsoft-Windows-Kernel-General        NT AUTHORITY\SYSTEM                zz-j       
12/2/2015        7:40:07        4        0        6        Microsoft-Windows-FilterManager        NT AUTHORITY\SYSTEM                zz-jo        0x0 6 1 8 FileInfo 2009-07-14T07:34:25.000000000Z
12/2/2015        7:40:14        4        0        36        VMnetAdapter        N/A        zz-juchao         
12/2/2015        7:40:14        4        0        2        MEIx64        N/A        zz-juchao         
12/2/2015        7:40:14        4        86        89        Microsoft-Windows-Kernel-Power        NT AUTHORITY\SYSTEM                zz-ju21 ACPI\ThermalZone\THRM 1 374 4 3 10000 333 0 0 0 0 0 0 0 0 0 377 0
12/2/2015        7:40:15        4        4        26        Microsoft-Windows-Kernel-Processor-Power        NT AUTHORITY\SYSTEM               
12/2/2015        7:40:15        4        4        26        Microsoft-Windows-Kernel-Processor-Power        NT AUTHORITY\SYSTEM               
12/2/2015        7:40:15        4        4        26        Microsoft-Windows-Kernel-Processor-Power        NT AUTHORITY\SYSTEM                z
12/2/2015        7:40:15        4        4        26        Microsoft-Windows-Kernel-Processor-Power        NT AUTHORITY\SYSTEM               
12/2/2015        7:40:15        4        0        18        BTHUSB        N/A        zz-juchao
作者: 回家路上    时间: 2015-12-2 16:49

  1. findstr "12/1/2015" 11.txt
复制代码

作者: ping192168101    时间: 2015-12-2 16:55

回复 2# 回家路上

您好,打扰下,我是想查询11.txt 里包含当天的行,不是固定某一天。比如当天日期12/2/2015这个日期变量如何取?多谢
作者: 回家路上    时间: 2015-12-2 17:13

本帖最后由 回家路上 于 2015-12-2 17:25 编辑
  1. set /a "y=%date:~0,4%,m=1%date:~5,2%-100,d=1%date:~8,2%-100"
  2. echo;%m%/%d%/%y%
复制代码

作者: ping192168101    时间: 2015-12-2 18:56

回复 4# 回家路上


完美解决,感谢您的回复。以后会经常来学习下。
作者: ping192168101    时间: 2015-12-2 20:59

本帖最后由 ping192168101 于 2015-12-2 21:02 编辑

回复 4# 回家路上
  1. @echo off
  2. rem ****log backup****
  3. if NOT EXIST %num% md %cd%\%num%
  4. rem ********
  5. Set k1=%Date:~0,4%%Date:~5,2%%Date:~8,2%
  6. set k2=%date:~0,4%-%date:~5,2%
  7. set /a "y=%date:~0,4%,m=1%date:~5,2%-100,d=1%date:~8,2%-100"
  8. rem ********
  9. if NOT EXIST  %num%\%k2% md %cd%\%num%\%k2%&&md %cd%\%num%\%k2%\sy
  10. rem ********
  11. cd %cd%\
  12. rem ********
  13. dumpel -l system -f %cd%\%num%\%k2%\sy\%k1%.txt
  14. type %cd%\%num%\%k2%\sy\%k1%.txt|find "%m%/%d%/%y%" >%cd%\%num%\%k2%\sy\file
  15. del %cd%\%num%\%k2%\sy\%k1%.txt
  16. rename %cd%\%num%\%k2%\sy\file %k1%.txt
  17. pause
复制代码
在执行这条时
del %cd%\%num%\%k2%\sy\%k1%.txt
提示找不到路径。没看出来问题在哪里?28,30行都执行不了

作者: 回家路上    时间: 2015-12-2 22:16

回复 6# ping192168101

那就
  1. echo %cd%\%num%\%k2%\sy\%k1%.txt
复制代码
看看有没有呀
作者: ping192168101    时间: 2015-12-3 13:39

回复 7# 回家路上

确定有此文件存在。昨天在本机测试失败。不过今天在服务器上脚本运行成功。感谢




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2