|
huanbei
初级用户
 
积分 32
发帖 14
注册 2008-10-26
状态 离线
|
『楼 主』:
[分享]批处理日历
使用 LLM 解释/回答一下
大家好,这里学习DOS和batch真是个不错的好地方。
我一直想找个dos下CAL命令,类似于linux下的cal。
后来再这里发现了批处理日历 http://www.cn-dos.net/forum/viewthread.php?tid=27739&fpage=1&highlight=%E6%97%A5%E5%8E%86&page=1这个帖子,用电脑也很久了,现在想稍微了解一下batch,正是通过这篇文章了解了xp下batch。感谢huzixuan和my3439955,一边学习代码一边消化,花了我几天的时间——发现真笨,现在自己改写了那篇文章的部分代码,原因如下:
1. 代码中有个临时文件,我觉得不爽。
2. 目前通用的日历排序是:日、一、二、三、四、五、六
3. 学习嘛,所以看看自己能否看懂,在这过程中不知不觉就改了
为什么想找一个cal命令呢,是因为我通常用USER登陆xp的,系统中自带的小日历只能是管理员才可打开(更改),这样让我很郁闷。
有错误的地方,请大家指正。
代码如下:
@echo off
setlocal enabledelayedexpansion
:::::::::::定义s1~s12,为每个月的天数
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
:::::::::::判断输入年份是否为闰年,更改2月的天数
set /p year=查几年?
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
::::::::::输入月份,并计算当年的第一天到上月月底的天数
set /p m=查几月?
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
::::::::::判断这一年的第一天是星期几?再判断所要查询月份的第一天是星期几?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo 日 一 二 三 四 五 六
:::::::::输入月份1号前的空日期
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
:::::::::找出要换行的日期(第一个,后每7个一换),显示当月日期并换行
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
程序有错,自己更正了一下。
Last edited by huanbei on 2008-11-6 at 00:53 ]
Hello everyone, this is really a great place to learn DOS and batch.
I've been wanting to find a CAL command under DOS, similar to cal under Linux.
Later, I found this batch calendar here http://www.cn-dos.net/forum/viewthread.php?tid=27739&fpage=1&highlight=%E6%97%A5%E5%8E%86&page=1 post. I've been using a computer for a long time, and now I want to understand batch a bit. It's through this article that I learned about batch under XP. Thanks to huzixuan and my3439955, I've been learning the code and digesting it. It took me a few days - I found myself really stupid. Now I've rewritten part of the code in that post for the following reasons:
1. There's a temporary file in the code, and I'm not happy with it.
2. The current general calendar sorting is: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
3. It's for learning, so I wanted to see if I could understand it, and I unknowingly changed it in the process.
Why did I want to find a cal command? Because I usually log in to XP with USER, and the small calendar that comes with the system can only be opened (changed) by the administrator, which makes me very depressed.
If there are any mistakes, please correct them for me.
The code is as follows:
@echo off
setlocal enabledelayedexpansion
::::::::::: Define s1~s12, the number of days in each month
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
::::::::::: Judge whether the input year is a leap year and change the number of days in February
set /p year=Which year to check?
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
:::::::::: Input the month and calculate the number of days from the first day of the year to the end of the previous month
set /p m=Which month to check?
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
:::::::::: Judge what day of the week the first day of this year is? Then judge what day of the week the first day of the month to be queried is?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo Sunday Monday Tuesday Wednesday Thursday Friday Saturday
::::::::: Input the empty dates before the 1st of the month
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
::::::::: Find the date to wrap (the first one, then every 7 one wrap), display the date of the current month and wrap
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
The program has errors, and I've corrected it myself.
Last edited by huanbei on 2008-11-6 at 00:53 ]
此帖被 +8 点积分 点击查看详情 评分人:【 HAT 】 | 分数: +4 | 时间:2008-11-5 22:01 | 评分人:【 BC 】 | 分数: +2 | 时间:2008-11-6 13:22 | 评分人:【 dosjj 】 | 分数: +2 | 时间:2008-11-7 07:42 |
|
|

用过DOS的人 |
|
2008-11-5 21:29 |
|
|
yishanju
银牌会员
     [b]看你妹啊[/b]
积分 1488
发帖 1357
注册 2006-5-20
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
网上不是有网页版的日历嘛
干嘛要找命令的。
There are web - based calendars on the Internet, so why look for a command - line one.
|

有问题请发论坛或者自行搜索,再短消息问我的统统是SB |
|
2008-11-5 22:14 |
|
|
yishanju
银牌会员
     [b]看你妹啊[/b]
积分 1488
发帖 1357
注册 2006-5-20
状态 离线
|
|
2008-11-5 22:21 |
|
|
huanbei
初级用户
 
积分 32
发帖 14
注册 2008-10-26
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by yishanju at 2008-11-5 22:14:
网上不是有网页版的日历嘛
干嘛要找命令的。
1. 学习DOS batch
2. 命令版的很有用,如果你的工作环境不能上网呢,并且非管理用户
3. 其短小精悍
4. 我们不是有Windows吗?干嘛用DOS?:)
Originally posted by yishanju at 2008-11-5 22:14:
There are web-based calendars on the internet, right?
Why look for a command-based one.
1. Learning DOS batch
2. Command-based is very useful. If your work environment can't access the internet, and you are a non-administrative user
3. It's compact and efficient
4. We have Windows, why use DOS? : )
|

用过DOS的人 |
|
2008-11-5 23:04 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 5 楼』:
Re 2楼
使用 LLM 解释/回答一下
在批处理室说这样的话,让人感觉很奇怪^_^
Saying such things in the batch processing room feels quite strange ^_^
|

 |
|
2008-11-6 00:58 |
|
|
BC
中级用户
  
积分 338
发帖 175
注册 2007-10-21
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
有个疑问,为什么不在最后加个pause呢...
There is a question, why not add a pause at the end...
|

C:\
C:\Del BC |
|
2008-11-6 13:22 |
|
|
huanbei
初级用户
 
积分 32
发帖 14
注册 2008-10-26
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by BC at 2008-11-6 13:22:
有个疑问,为什么不在最后加个pause呢...
让程序自动返回DOS命令符不好吗?
我是看到原来的程序中最后用了pause>nul,但是相对于本程序而言,这样有什么实际的作用呢?显示结束后,要回到提示符还要再按一下空格。
Originally posted by BC at 2008-11-6 13:22:
There is a question, why not add a pause at the end...
Is it not good to let the program automatically return to the DOS command prompt?
I saw that the original program used pause>nul at the end, but what is the actual function of this for this program? After the display ends, you have to press the space bar again to return to the prompt.
|

用过DOS的人 |
|
2008-11-6 19:00 |
|
|
BC
中级用户
  
积分 338
发帖 175
注册 2007-10-21
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
哦...原来如此,请兄原谅我的鲁莽.要是搞个dos兼容的就好看了...
Oh... So that's how it is, please forgive my rashness, brother. It would be nice to have a DOS-compatible one...
|

C:\
C:\Del BC |
|
2008-11-6 21:35 |
|
|
huanbei
初级用户
 
积分 32
发帖 14
注册 2008-10-26
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
刚才读了 批处理参数问题一点谈
http://www.cn-dos.net/forum/viewthread.php?tid=17785&fpage=1&highlight=%E5%8F%82%E6%95%B0学习了一下。
为日历加入了参数。效果如下:
新代码如下:
@echo off
setlocal enabledelayedexpansion
:::::加入batch参数功能
set year=%~1
set m=%~2
if not defined m (
if not defined year (
echo 输入格式 cal year month 例:cal 2008 10
) else (
echo 输入格式 cal year month 例:cal 2008 10
)
) else (
call :main
)
goto :eof
:main
:::::::::::定义s1~s12,为每个月的天数
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
:::::::::::判断输入年份是否为闰年,更改2月的天数
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
::::::::::输入月份,并计算当年的第一天到上月月底的天数
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
::::::::::判断这一年的第一天是星期几?再判断所要查询月份的第一天是星期几?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo.
echo.
echo %year% %m%
echo.
echo.
echo 日 一 二 三 四 五 六
:::::::::输入月份1号前的空日期
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
:::::::::找出要换行的日期(第一个,后每7个一换),显示当月日期并换行
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
Just now I read A Little Talk about Batch Processing Parameter Issues
http://www.cn-dos.net/forum/viewthread.php?tid=17785&fpage=1&highlight=%E5%8F%82%E6%95%B0 and learned something.
Added parameters to the calendar. The effect is as follows:
The new code is as follows:
@echo off
setlocal enabledelayedexpansion
:::::Add batch parameter function
set year=%~1
set m=%~2
if not defined m (
if not defined year (
echo Input format: cal year month, e.g.: cal 2008 10
) else (
echo Input format: cal year month, e.g.: cal 2008 10
)
) else (
call :main
)
goto :eof
:main
::::::::::Define s1~s12, the number of days in each month
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
::::::::::Judge whether the input year is a leap year, change the number of days in February
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
::::::::::Input month, and calculate the number of days from the first day of the year to the end of last month
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
::::::::::Judge what day of the week the first day of this year is? Then judge what day of the week the first day of the month to be queried is?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo.
echo.
echo %year% %m%
echo.
echo.
echo 日 一 二 三 四 五 六
:::::::::Empty dates before the 1st of the input month
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
:::::::::Find the date to wrap (the first one, then every 7 to wrap), display the current month's date and wrap
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
此帖被 +2 点积分 点击查看详情 评分人:【 HAT 】 | 分数: +2 | 时间:2008-11-7 01:02 |
|
|

用过DOS的人 |
|
2008-11-6 21:59 |
|
|
huanbei
初级用户
 
积分 32
发帖 14
注册 2008-10-26
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
发现那篇帖子讨论的真深啊,真佩服无奈何、willsort版主的钻研精神,从中现学现用。
I find that post really in - depth in discussion. I really admire the research spirit of moderators Wunaike and willsort. I learn from it and apply it now.
|

用过DOS的人 |
|
2008-11-6 22:04 |
|
|
ucdos12
新手上路

积分 11
发帖 11
注册 2008-11-7
状态 离线
|
|
2008-11-7 15:13 |
|
|
yyyyyyyyy
初级用户
 
积分 137
发帖 85
注册 2007-11-26
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
润色了一下,增强了:
避免有好事者输英文
默认一月
@echo off
setlocal enabledelayedexpansion
:::::加入batch参数功能
set year=%~1
set m=%~2
set /a year1=!year!+1-1
set /a m1=!m!+1-1
if not "!year!"=="!year1!" goto help
if not "!m!"=="!m!" goto help
if not defined m (
if not defined year (
goto :help
) else (
set m=1
goto :main
)
)
:help
@echo 输入格式:
@echo cal year month
@echo 例:cal 2008 10
@echo 如未输入月份,则默认为1月
exit /b
:main
:::::::::::定义s1~s12,为每个月的天数
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
:::::::::::判断输入年份是否为闰年,更改2月的天数
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
::::::::::输入月份,并计算当年的第一天到上月月底的天数
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
::::::::::判断这一年的第一天是星期几?再判断所要查询月份的第一天是星期几?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo.
echo.
echo %year% %m%
echo.
echo.
echo 日 一 二 三 四 五 六
:::::::::输入月份1号前的空日期
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
:::::::::找出要换行的日期(第一个,后每7个一换),显示当月日期并换行
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
Polished and enhanced:
Avoid troublemakers inputting English
Default to January
@echo off
setlocal enabledelayedexpansion
:::::Add batch parameter functionality
set year=%~1
set m=%~2
set /a year1=!year!+1-1
set /a m1=!m!+1-1
if not "!year!"=="!year1!" goto help
if not "!m!"=="!m!" goto help
if not defined m (
if not defined year (
goto :help
) else (
set m=1
goto :main
)
)
:help
@echo Input format:
@echo cal year month
@echo Example: cal 2008 10
@echo If no month is input, it defaults to January
exit /b
:main
:::::::::::Define s1~s12, the number of days in each month
for %%b in (31,28,31,30,31,30,31,31,30,31,30,31) do (
set /a y+=1
set s!y!=%%b
)
:::::::::::Determine if the input year is a leap year, change the number of days in February
set /a year_chk1=year%%400
set /a year_chk2=year%%4
set /a year_chk3=year%%100
if %year_chk1% equ 0 set s2=29
if %year_chk2% equ 0 if %year_chk3% neq 0 set s2=29
::::::::::Input the month and calculate the number of days from the first day of the year to the end of the previous month
set n=%m%
set /a n-=1
for /l %%c in (1,1,%n%) do (
set /a day+=!s%%c!)
::::::::::Determine what day of the week the first day of this year is? Then determine what day of the week the first day of the month to be queried is?
set /a yearflag=(%year%+(%year%-1)/4-(%year%-1)/100+(%year%-1)/400)%%7
if %m%==1 (set monthflag=%yearflag% ) else (set /a monthflag=%yearflag%+!day!%%7 )
if %monthflag% geq 7 (set /a monthflag-=7)
echo.
echo.
echo %year% %m%
echo.
echo.
echo Sun Mon Tue Wed Thu Fri Sat
:::::::::Empty dates before the 1st of the input month
for /l %%d in (1,1,%monthflag%) do (
set /p= <nul)
:::::::::Find the date to wrap (the first one, then every 7 one wrap), display the current month's date and wrap
set /a cl=7-%monthflag%
for /l %%e in (1 1 !s%m%!) do (
if %%e lss 10 (set /p= %%e<nul) else (set /p= %%e<nul)
if %%e==!cl! (echo.&set /a cl+=7)
)
echo.
|
|
2008-11-7 20:27 |
|
|