China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-10 10:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Share] Batch Calendar View 2,748 Replies 11
Original Poster Posted 2008-11-05 21:29 ·  中国 安徽 合肥 联通
初级用户
Credits 32
Posts 14
Joined 2008-10-26 20:01
17-year member
UID 129177
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
HAT +4 2008-11-05 22:01
BC +2 2008-11-06 13:22
dosjj +2 2008-11-07 07:42
用过DOS的人
Floor 2 Posted 2008-11-05 22:14 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
There are web - based calendars on the Internet, so why look for a command - line one.

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 3 Posted 2008-11-05 22:21 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
Here are several software that can be used as calendars.

http://www.time.ac.cn/serve/down.htm

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 4 Posted 2008-11-05 23:04 ·  中国 安徽 合肥 联通
初级用户
Credits 32
Posts 14
Joined 2008-10-26 20:01
17-year member
UID 129177
Gender Male
Status Offline
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的人
Floor 5 Posted 2008-11-06 00:58 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Saying such things in the batch processing room feels quite strange ^_^
Floor 6 Posted 2008-11-06 13:22 ·  中国 广东 江门 电信
中级用户
★★
Credits 338
Posts 175
Joined 2007-10-21 15:30
18-year member
UID 100351
Gender Male
Status Offline
There is a question, why not add a pause at the end...
Floor 7 Posted 2008-11-06 19:00 ·  中国 安徽 合肥 联通
初级用户
Credits 32
Posts 14
Joined 2008-10-26 20:01
17-year member
UID 129177
Gender Male
Status Offline
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的人
Floor 8 Posted 2008-11-06 21:35 ·  中国 广东 江门 电信
中级用户
★★
Credits 338
Posts 175
Joined 2007-10-21 15:30
18-year member
UID 100351
Gender Male
Status Offline
Oh... So that's how it is, please forgive my rashness, brother. It would be nice to have a DOS-compatible one...
Floor 9 Posted 2008-11-06 21:59 ·  中国 安徽 芜湖 联通
初级用户
Credits 32
Posts 14
Joined 2008-10-26 20:01
17-year member
UID 129177
Gender Male
Status Offline
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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +2 2008-11-07 01:02
用过DOS的人
Floor 10 Posted 2008-11-06 22:04 ·  中国 安徽 芜湖 联通
初级用户
Credits 32
Posts 14
Joined 2008-10-26 20:01
17-year member
UID 129177
Gender Male
Status Offline
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的人
Floor 11 Posted 2008-11-07 15:13 ·  中国 重庆 沙坪坝区 电信
新手上路
Credits 11
Posts 11
Joined 2008-11-07 14:45
17-year member
UID 130285
Gender Male
Status Offline
Not bad, collect and use
Floor 12 Posted 2008-11-07 20:27 ·  中国 江苏 无锡 江阴市 电信
初级用户
★★
Credits 137
Posts 85
Joined 2007-11-26 21:05
18-year member
UID 103862
Gender Male
Status Offline
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.
Forum Jump: