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-06-23 14:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch version perpetual calendar DigestI View 16,195 Replies 44
Floor 31 Posted 2007-08-02 07:19 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
18-year member
UID 93679
Gender Male
Status Offline
I clicked [Copy to clipboard] on the 20th floor, copied it into a text file, then changed the extension.

How could there be no problem? Try copying your own code into a bat file again, then run it and enter the example given in it. It will report an error. At that point the error screen just loops forever, no matter whether you press any key or not.
Floor 32 Posted 2007-08-02 07:55 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Your case should be a bit special
Neither I nor namejm earlier ran into this problem in our discussion
This time, just for testing, I deliberately used IE to open this page
and tried the [Copy to clipboard] function
and there was no problem at all

So please still follow the suggestion and insert a debugging statement to look at the result first before saying more
Floor 33 Posted 2007-08-02 13:31 ·  新加坡 Verizon通讯公司UUNet互联点
初级用户
Credits 88
Posts 43
Joined 2007-05-30 22:09
19-year member
UID 89807
Gender Male
Status Offline
The function should be fine
I used the function for some code before too

This time, just for testing, I deliberately used IE to open this page

Then what does qzwqzw use to browse the web?
firefox or opera?~~
Floor 34 Posted 2007-08-02 16:47 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
18-year member
UID 93679
Gender Male
Status Offline
http://upload.programfan.com/upfile/200708021645310.rar
Try this one of mine and see whether it has this problem. I copied it down completely as-is.

Just type any one letter and press Enter. It says "Invalid date." Then no matter what key you press, it won't return to the original screen.
Floor 35 Posted 2007-08-03 07:03 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Many thanks for your report!
Because of it I uncovered another secret of Microsoft's cmd

The solution to your problem is very simple
Please change your code file's extension to .cmd
or add a line cd. before set /a in the code, to reset the error code to 0

==================================

Now let me describe the process of solving the problem
First, after downloading your test file
I tested it and indeed found the problem you described

Then I compared the problem code with my source code that runs normally
and found that the problem code had two extra spaces at the ends of line 2 and the line containing the first goto Main
Because of the special position of the 2nd space
I thought I had found the cause of the problem

Because I use EditPlus to edit source code
it automatically clears trailing spaces when saving
and the code posted to the forum might have gained them after being modified with some other editor

However, after removing the extra spaces and testing, I found the problem still existed
A binary comparison of the two code files again showed they were completely identical

Then I began a long tracing and debugging process
I found that once the problem code generated errorlevel 9167 because of an error in set /a
it would no longer reset errorlevel to 0 because of a correct set /a later
while the normal code did not have this problem

After thinking it over again and again
I finally determined that this problem is related to the file extension
That is,
in a .bat file, an error generated by set will never be reset by set itself
while in a .cmd file, it can be

Here is a piece of test code
Save it separately as .bat and .cmd files and test it
you will find the results differ
At this point the issue is basically clear


@echo off
set /a ii=23 + 12
echo %errorlevel%.
set /a ii=23 + 09
echo %errorlevel%.
echo %errorlevel%.
set ii=23
echo %errorlevel%.
pause


But as for the deeper reason for this problem, I still haven't found it
I'll leave that for interested people to continue exploring
Also, when I ran the .bat file from the EditPlus toolbar, I did not encounter this problem
Floor 36 Posted 2007-08-03 20:04 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
18-year member
UID 93679
Gender Male
Status Offline
Then please update the code on the 20th floor to make it a bit more complete.
As long as it can be guaranteed to run normally, whether in bat or cmd, that's enough.
Floor 37 Posted 2007-08-04 18:05 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
18-year member
UID 93679
Gender Male
Status Offline
There is still an error. When saved as BAT, entering 07-02-03 still reports an error.

I copied the code from the 20th floor and tested it separately in BAT and CMD.
Floor 38 Posted 2007-08-04 19:02 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
The problem has already been identified
It is still the set errorlevel issue

As for the details of the problem
and the code modification
I'll leave that as your homework for tonight

Consider it a test of your understanding of this issue
Floor 39 Posted 2007-08-04 19:14 ·  中国 广东 梅州 电信
中级用户
★★
Credits 262
Posts 129
Joined 2007-07-11 06:50
18-year member
UID 93679
Gender Male
Status Offline
No way, I'm not very familiar with batch files, not as knowledgeable and experienced as you guys are.

If you ask me to test, that's fine, but if you ask me to fix the problem, that's really making things hard for me...
Floor 40 Posted 2007-08-04 23:20 ·  中国 广东 广州 番禺区 电信
初级用户
Credits 82
Posts 15
Joined 2007-05-26 20:14
19-year member
UID 89403
Gender Male
Status Offline
Brother qzwqzw may be too busy...
I'll just modify it according to the symptom. It may bring out even more problems, hehe~`
Also added zodiac and Heavenly Stem/Earthly Branch year calculation for the lunar calendar.

:: Monthly calendar query tool China DOS Union special edition. Please indicate copyright when reposting
:: Original: zjl5 Updated: namejm, qzwqzw 2007-06-03

:: Algorithm: Kim Larsen calculation formula
:: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
:: Treat January and February as the 13th and 14th months of the previous year
:: Example: if it is 2004-1-10 then convert it to: 2003-13-10 and substitute it into the formula.

:: The input date format is: year-month-day (- can be replaced with : or /, and they can be mixed)
:: In the calendar, ★=today

:: Supports multiple date input formats:
:: ① If only one number is entered, it is treated as querying the month of the current year; the last two digits are automatically taken for the query, and ★ is marked on the 1st;
:: ② If two numbers are entered, it is treated as querying the year and month, and ★ is marked on the 1st;
:: ③ If the full date is entered, ★ is marked on the specified date

:: About year conversion:
:: ① If the year entered has fewer than three digits, it is converted as follows:
:: 50~99 is judged as 19xx
:: 0~49 is judged as 20xx
:: ② If the entered year has more than two digits, then take the last four characters (pad the high end with 0 if needed),
:: and calculate the date of that year according to rule ①;

@echo off
color 3F
mode con cols=40 lines=20
setlocal enabledelayedexpansion
set str=SunMonTueWedThuFriSat
set sdate=%date%

:Main
cls&echo.
:: Date extraction, formatting, and validation
for /f "tokens=1,2,3 delims=-/: " %%i in ("%sdate%") do (
(set sy=%%i) && (set sm=%%j) && (set sd=%%k))
if %sm% geq 13 (echo Invalid date.&pause>nul&(set sdate=%date%)&pause>nul&goto :main)
if %sd% geq 32 (echo Invalid date.&pause>nul&(set sdate=%date%)&pause>nul&goto :main)
if not defined sd set sd=1
if not defined sm set sm=%sy%& set sy=%date:~0,4%
(set sy=0000%sy%) && (set sm=00%sm%) && (set sd=00%sd%)
(set sy=%sy:~-4%) && (set sm=%sm:~-2%) && (set sd=%sd:~-2%)
set /a y=1%sy%-10000, m=1%sm%-100, d=1%sd%-100 2>nul
if %y% lss 100 (
if %y% lss 50 (set /a y+=2000) else (set /a y+=1900)
set sy=!y!
)
if %m% lss 13 if %d% lss 32 goto Calc

:Calc
set sx=MonkeyRoosterDogPigRatOxTigerRabbitDragonSnakeHorseGoat
set tg=GengXinRenGuiJiaYiBingDingWuJi
set dz=ShenYouXuHaiZiChouYinMaoChenSiWuWei
set /a sxnum=%sy% %% 12
set /a tgnum=%sy:~-1%
title Lunar calendar !tg:~%tgnum%,1!!dz:~%sxnum%,1! year Zodiac:!sx:~%sxnum%,1!
:: Calculate the number of days in each month
set days=31
for %%i in (4 6 9 11) do if %m% equ %%i set days=30
:: Calculate February adjustment
set /a leap="^!(y%%4) & ^!(^!(y%%100)) | ^!(y%%400)"
if %m% equ 2 set /a days=28+%leap%
if %m% leq 2 (set /a y-=1& set /a m+=12)
:: Calculate the weekday of the specified date
set /a w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%%7

echo. %sy%-%sm% Query date:%sy%-%sm%-%sd%,week !str:~%w%,1!
echo.
:: Generate the monthly calendar
set /a wb=(w+35-d) %% 7, we=wb+days+1, day=1
echo. Sun Mon Tue Wed Thu Fri Sat
echo. ━━━━━━━━━━━━━━━━━━━
set /p= <nul
for /l %%i in (0,1,37) do (
set "temp= "
if %%i GTR %wb% if %%i LSS %we% (
set temp= !day!
set temp=!temp:~-2!
if !d! EQU !day! set temp=★
set /a day+=1
)
set /p= !temp!<nul
set /a "wm=(%%i+1)%%7"
if !wm! equ 0 echo.&echo.&set /p= <nul
)
echo.
echo ━━━━━━━━━━━━━━━━━━━
echo. Enter a date to query the weekday and display that month's calendar
echo.
set sdate=
set /p sdate= Format like: 07-02-03, Exit:
if defined sdate goto Main
Floor 41 Posted 2007-08-05 01:21 ·  中国 广东 广州 番禺区 电信
初级用户
Credits 82
Posts 15
Joined 2007-05-26 20:14
19-year member
UID 89403
Gender Male
Status Offline
Strange, why is %errorlevel% 9167? Please explain, sir, thanks!!
Since I don't have permission to edit the post, I have to make another post.

:: Monthly calendar query tool China DOS Union special edition. Please indicate copyright when reposting
:: Original: zjl5 Updated: namejm, qzwqzw 2007-06-03

:: Algorithm: Kim Larsen calculation formula
:: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
:: Treat January and February as the 13th and 14th months of the previous year
:: Example: if it is 2004-1-10 then convert it to: 2003-13-10 and substitute it into the formula.

:: The input date format is: year-month-day (- can be replaced with : or /, and they can be mixed)
:: In the calendar, ★=today

:: Supports multiple date input formats:
:: ① If only one number is entered, it is treated as querying the month of the current year; the last two digits are automatically taken for the query, and ★ is marked on the 1st;
:: ② If two numbers are entered, it is treated as querying the year and month, and ★ is marked on the 1st;
:: ③ If the full date is entered, ★ is marked on the specified date

:: About year conversion:
:: ① If the year entered has fewer than three digits, it is converted as follows:
:: 50~99 is judged as 19xx
:: 0~49 is judged as 20xx
:: ② If the entered year has more than two digits, then take the last four characters (pad the high end with 0 if needed),
:: and calculate the date of that year according to rule ①;

@echo off
color 3F
mode con cols=40 lines=20
setlocal enabledelayedexpansion
set str=SunMonTueWedThuFriSat
set sdate=%date%

:Main
cls&echo.
:: Date extraction, formatting, and validation
for /f "tokens=1,2,3 delims=-/: " %%i in ("%sdate%") do (
(set sy=%%i) && (set sm=%%j) && (set sd=%%k)
)
if not defined sd set sd=1
if not defined sm set sm=%sy%& set sy=%date:~0,4%

(set sy=0000%sy%) && (set sm=00%sm%) && (set sd=00%sd%)
(set sy=%sy:~-4%) && (set sm=%sm:~-2%) && (set sd=%sd:~-2%)
set /a y=1%sy%-10000, m=1%sm%-100, d=1%sd%-100 2>nul
if errorlevel 9167 goto Error
if %y% lss 100 (
if %y% lss 50 (set /a y+=2000) else (set /a y+=1900)
set sy=!y!
)
if %m% lss 13 if %d% lss 32 goto Calc

:Error
echo.Invalid date.
pause>nul
cd.
set sdate=%date%
goto Main

:Calc
set sx=MonkeyRoosterDogPigRatOxTigerRabbitDragonSnakeHorseGoat
set tg=GengXinRenGuiJiaYiBingDingWuJi
set dz=ShenYouXuHaiZiChouYinMaoChenSiWuWei
set /a sxnum=%sy% %% 12
set /a tgnum=%sy:~-1%
title Lunar calendar !tg:~%tgnum%,1!!dz:~%sxnum%,1! year Zodiac:!sx:~%sxnum%,1!
:: Calculate the number of days in each month
set days=31
for %%i in (4 6 9 11) do if %m% equ %%i set days=30
:: Calculate February adjustment
set /a leap="^!(y%%4) & ^!(^!(y%%100)) | ^!(y%%400)"
if %m% equ 2 set /a days=28+%leap%
if %m% leq 2 (set /a y-=1& set /a m+=12)
:: Calculate the weekday of the specified date
set /a w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%%7

echo. %sy%-%sm% Query date:%sy%-%sm%-%sd%,week !str:~%w%,1!
echo.
:: Generate the monthly calendar
set /a wb=(w+35-d) %% 7, we=wb+days+1, day=1
echo. Sun Mon Tue Wed Thu Fri Sat
echo. ━━━━━━━━━━━━━━━━━━━
set /p= <nul
for /l %%i in (0,1,37) do (
set "temp= "
if %%i GTR %wb% if %%i LSS %we% (
set temp= !day!
set temp=!temp:~-2!
if !d! EQU !day! set temp=★
set /a day+=1
)
set /p= !temp!<nul
set /a "wm=(%%i+1)%%7"
if !wm! equ 0 echo.&echo.&set /p= <nul
)
echo.
echo ━━━━━━━━━━━━━━━━━━━
echo. Enter a date to query the weekday and display that month's calendar
echo.
set sdate=
set /p sdate= Format like: 07-02-03, Exit:
if defined sdate goto Main
Floor 42 Posted 2007-08-05 18:47 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
9167 is the errorlevel generated by set /a because of an invalid number (for example 09)
What needs attention is that set /p will also generate an errorlevel of 1 if it receives no input at all
And even if set /a and set /p run normally afterward, they still will not reset the earlier errorlevel

That is to say, in the command line or in a .bat script
the set command only sets errorlevel to a non-0 value when it runs with an error
when it runs correctly, it does not set it to 0

But in a .cmd script
set will set errorlevel to 0 when it runs correctly
Floor 43 Posted 2007-08-20 15:30 ·  中国 广东 深圳 电信
贫困用户
Credits -4
Posts 5
Joined 2007-08-20 13:50
18-year member
UID 95409
Gender Male
Status Offline
It would be even better if it could check the lunar calendar too
Floor 44 Posted 2007-08-27 11:16 ·  中国 广东 湛江 联通
初级用户
Credits 22
Posts 11
Joined 2006-09-01 00:08
19-year member
UID 61658
Status Offline
Very good, adding to my collection
Floor 45 Posted 2008-05-26 14:14 ·  中国 北京 鹏博士BGP
新手上路
Credits 8
Posts 5
Joined 2008-02-02 19:34
18-year member
UID 110061
Gender Male
Status Offline
I was really happy to come here today and see a message from namejm. I've always been following your news. It's good that you're okay.
Forum Jump: