[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

  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, 帮忙看看是哪里的问题?

我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

厉害,调用外部的函数库就这么简单。。。

TOP

假设今天是9月1日,那么下列代码就能返回8月31日,至于具体的日期形式,要靠 get-date -format xxx 参数控制。
powershell代码
  1. (get-date).AddDays(-1)
复制代码
脚本是写给人看的,是写给用户看的,而不是写给机子看的
用户能看懂、会修改的脚本,才是好脚本。
写易懂的powershell脚本帮人解决问题,进而让用户学会自渔,吾所愿也

TOP

回复 4# PowerShell
  1. (Get-Date -uformat "%Y-%m-%d").AddDays(-1)
复制代码
使用了format之后,get-date的结果就从日期时间对象编程普通字符串了,无法直接调用其AddDays方法,这个情况如何处理呢?

TOP

Get-Date (get-date).AddDays(-1)  -uformat "%Y-%m-%d"
脚本是写给人看的,是写给用户看的,而不是写给机子看的
用户能看懂、会修改的脚本,才是好脚本。
写易懂的powershell脚本帮人解决问题,进而让用户学会自渔,吾所愿也

TOP

返回列表