找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 26821|回复: 5

[日期时间] 取前一天日期建文件夹名的批处理为何出错?

[复制链接]
发表于 2013-9-3 08:27:25 | 显示全部楼层 |阅读模式
  1. @echo on
  2. setlocal enabledelayedexpansion
  3. set curr_year=%date:~0,4%
  4. set curr_month=%date:~5,2%  
  5. set curr_day=%date:~8,2%

  6. if %curr_month:~0,1%==0 set curr_month=%curr_month:~1,1%
  7. if %curr_day:~0,1%==0 set curr_day=%curr_day:~1,1%

  8. set last_year=%curr_year%
  9. set tmp_last_month=%curr_month%
  10. set /a tmp_last_day=%curr_day%-1  

  11. if %tmp_last_day%==0 set /a tmp_last_month=%curr_month%-1

  12. if !tmp_last_day!==0 (
  13. if !tmp_last_month!==0 (
  14. set last_day=31
  15. goto final
  16. )  
  17. if !tmp_last_month!==1 (
  18. set last_day=31
  19. goto final
  20. )  
  21. if !tmp_last_month!==2 (
  22. goto leap_year
  23. )  
  24. if !tmp_last_month!==3 (
  25. set last_day=31
  26. goto final  
  27. )  
  28. if !tmp_last_month!==4 (
  29. set last_day=30
  30. goto final  
  31. )  
  32. if !tmp_last_month!==5 (
  33. set last_day=31
  34. goto final  
  35. )  
  36. if !tmp_last_month!==6 (
  37. set last_day=30
  38. goto final
  39. )  
  40. if !tmp_last_month!==7 (
  41. set last_day=31
  42. goto final  
  43. )  
  44. if !tmp_last_month!==8 (
  45. set last_day=31
  46. goto final  
  47. )  
  48. if !tmp_last_month!==9 (
  49. set last_day=30
  50. goto final
  51. )  
  52. if !tmp_last_month!==10 (
  53. set last_day=31  
  54. goto final  
  55. )  
  56. if !tmp_last_month!==11 (
  57. set last_day=30
  58. goto final  
  59. )  
  60. ) else (goto :final)

  61. :leap_year
  62. set leapyear_flag=0  
  63. set /a isleapyear=%curr_year%%%4  
  64. if %isleapyear%==0 set leapyear_flag=1
  65. set /a isleapyear=%curr_year%%%400  
  66. if %isleapyear%==0 set leapyear_flag=1
  67. set /a isleapyear=%curr_year%%%100  
  68. if %isleapyear%==0 set leapyear_flag=0
  69. set tmp_last_day=28  
  70. if %leapyear_flag%==1 set tmp_last_day=29


  71. :final
  72. if %tmp_last_month%==0 (
  73. set /a last_year=%curr_year%-1
  74. set last_month=12
  75. )

  76. set last_month=%temp_last_month%
  77. if %tmp_last_month%==1 set last_month=01
  78. if %tmp_last_month%==2 set last_month=02
  79. if %tmp_last_month%==3 set last_month=03
  80. if %tmp_last_month%==4 set last_month=04
  81. if %tmp_last_month%==5 set last_month=05
  82. if %tmp_last_month%==6 set last_month=06
  83. if %tmp_last_month%==7 set last_month=07
  84. if %tmp_last_month%==8 set last_month=08
  85. if %tmp_last_month%==9 set last_month=09

  86. set last_day=%tmp_last_day%
  87. if %tmp_last_day%==1 set last_day=01
  88. if %tmp_last_day%==2 set last_day=02
  89. if %tmp_last_day%==3 set last_day=03
  90. if %tmp_last_day%==4 set last_day=04
  91. if %tmp_last_day%==5 set last_day=05
  92. if %tmp_last_day%==6 set last_day=06
  93. if %tmp_last_day%==7 set last_day=07
  94. if %tmp_last_day%==8 set last_day=08
  95. if %tmp_last_day%==9 set last_day=09

  96. set dt=%last_year:~0,4%-%last_month:~0,2%-%last_day:~0,2%

  97. md d:\%dt%
复制代码
每次都执行到 leap_year, 帮忙看看是哪里的问题?
发表于 2013-9-3 08:33:36 | 显示全部楼层
 楼主| 发表于 2013-9-3 08:48:33 | 显示全部楼层
厉害,调用外部的函数库就这么简单。。。
发表于 2013-9-3 09:39:37 | 显示全部楼层
假设今天是9月1日,那么下列代码就能返回8月31日,至于具体的日期形式,要靠 get-date -format xxx 参数控制。
powershell代码
  1. (get-date).AddDays(-1)
复制代码
发表于 2013-9-3 13:04:48 | 显示全部楼层
回复 4# PowerShell
  1. (Get-Date -uformat "%Y-%m-%d").AddDays(-1)
复制代码
使用了format之后,get-date的结果就从日期时间对象编程普通字符串了,无法直接调用其AddDays方法,这个情况如何处理呢?
发表于 2013-9-3 13:31:15 | 显示全部楼层
Get-Date (get-date).AddDays(-1)  -uformat "%Y-%m-%d"
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 14:44 , Processed in 0.017830 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表