このコードの利点は、現在の日付をdate.txt
次のように入力できることです。
echo %date%>date.txt
次に、その日付に基づいて行動するには、次の行に続くコードのみが必要です。
set lastmonth=
コードは次のとおりです。
@echo off
:Beginagain
set lastmonth= %date:~4,2%
set /a lastmonth=%lastmonth: 0=%-1
echo.
set /p lastmonth=Enter month for date.txt [1-12, Default = %lastmonth%, Q = Quit]:
if %lastmonth%==Q goto :eof
if %lastmonth%==q goto :eof
set year=%date:~-4%
if %lastmonth% leq 0 (
set lastmonth=12
set /a year=year-1
)
if %lastmonth% lss 10 set lastmonth=0%lastmonth%
echo %date:~,4%%lastmonth%/%date:~7,2%/%year% > date.txt
set year=
set lastmonth=
set /p olddate=<date.txt
set olddate= %olddate:~4,2%
set olddate=%olddate: 0=%
set thismonth= %date:~4,2%
set thismonth=%thismonth: 0=%
set /a diff=thismonth-olddate
if %diff% lss 0 (
echo Date from Last Year
) else if %diff%==0 (
echo Date from this month
) else if %diff%==1 (
echo Date from last month
) else if %diff% gtr 1 (
echo Date from before last month
)
goto :Beginagain
上記のコードを使用すると、[Q] uitを選択するまでループすることで、さまざまな月を試すことができます。
デフォルトオプションを使用すると、常に次のように出力されます。
Date from last month
9月中に実行すると、上記のコードは次のように表示されます。
Enter month for date.txt [1-12, Default = 8, Q = Quit]:
1から12までの任意の月(前年のように機能する上または下の任意の数字)を入力できる場合はENTER、デフォルトの月を押すか、入力QENTERしてプログラムを終了します。
デフォルトの月は、常に現在の月の1か月前になります。したがって、現在の月が1月の場合、デフォルトは12になります。現在の月が12月の場合、デフォルトは11になります。