![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-28 20:25 |
47,813 topics / 349,918 posts / today 0 new / 48,277 members |
| DOS批处理 & 脚本技术(批处理室) » [Original] Batch script to get the previous day's date |
| Printable Version 3,367 / 10 |
| Floor1 mayining | Posted 2008-01-17 10:57 |
| 新手上路 Posts 2 Credits 6 | |
|
This is part of a bat I just wrote a few days ago. This is my first time writing a bat script. Those days I was often soaking in the union to learn bat knowledge, and I'm very grateful to the brothers in the union!
Since batch scripts are very widely used in automatic file uploading, getting the date of the previous day is a problem that is mostly encountered when completing this task, so I post the code here for newbies like me to refer to. My method is a bit of a trick: if today is the 1st, then I need to handle the month (maybe the year), and then list the last day of the previous month. For example, October must definitely have 31 days, but February needs special handling. I need to calculate whether it's a leap year. The specific calculation method can be seen in my code. I'm sure this won't stump everyone. Finally, I just put the date together to get the date of the previous day. I hope everyone has a better method and post the code to share it :) |
|
| Floor2 daker | Posted 2008-01-17 12:27 |
| 初级用户 Posts 17 Credits 34 | |
|
Why not use for /l???
|
|
| Floor3 honghunter | Posted 2008-01-17 13:06 |
| 中级用户 Posts 135 Credits 321 | |
|
Give a like, thanks for sharing.
|
|
| Floor4 daker | Posted 2008-01-17 14:39 |
| 初级用户 Posts 17 Credits 34 | |
|
I used FOR /L to modify it, just for my own practice~~~~ It should be more concise~~
|
|
| Floor5 HAT | Posted 2008-01-17 15:14 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Find the date N days ago
http://www.cn-dos.net/forum/viewthread.php?tid=31296 |
|
| Floor6 mayining | Posted 2008-01-17 16:30 |
| 新手上路 Posts 2 Credits 6 | |
|
Well, Brother daker, I have carefully read your code. It's indeed much more concise than mine. I didn't consider that using for /l could solve the problem of known - length strings at that time. Thanks a lot! I've also looked at Brother HAT's code. His code is more powerful in functionality, and the solutions 2 and 3 deal with it from the mathematical essence of dates, which feels much more professional. I think when I have time, I'll write a script calendar program, so that I can fundamentally solve the defects of the date function in bat. I wonder if someone has already done it? Well, closing the thread. Thanks to everyone again.
|
|
| Floor7 fzp1978321 | Posted 2008-03-06 12:03 |
| 初级用户 Posts 50 Credits 110 | |
|
Then how to get the date of the previous day under WIN2000? These are all under XP environment.
|
|
| Floor8 xaleibin | Posted 2008-10-01 04:52 |
| 新手上路 Posts 2 Credits 4 | |
|
You should add a line "setlocal enabledelayedexpansion" at the front of the batch file, which means enabling variable delay; otherwise, this program will not execute normally. I have tried it. Finally, thank you for sharing, which helped me solve the problem.
|
|
| Floor9 xaleibin | Posted 2008-10-01 04:54 |
| 新手上路 Posts 2 Credits 4 | |
|
Only when variable delay is enabled can you use !! to enclose the variable name to represent the reference to the variable value.
|
|
| Floor10 yanglei | Posted 2009-11-05 22:26 |
| 新手上路 Posts 4 Credits 7 | |
|
If there is a database connection, you can take sysdate - 1 from the database, write it to a file, and then take out the date of the previous day from the file... The premise is that you can connect to the database, heh heh
|
|
| Floor11 hbby | Posted 2009-12-16 15:51 |
| 中级用户 Posts 99 Credits 220 | |
|
@echo off
echo Wscript.echo dateadd("d",-1,date)>vbs.vbs for /f %%a in ('cscript //nologo vbs.vbs') do del vbs.vbs&&echo %%a |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |