中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
作者:
标题: [原创]日期运算的批处理 上一主题 | 下一主题
genteman
初级用户





积分 90
发帖 43
注册 2007-10-31
状态 离线
『楼 主』:  [原创]日期运算的批处理

由于刚接触P,出于练手的目的,编写了一个日期函数,该P主要通过输入初始日期参数,分析运算加1后返回给返回参数,同时考虑到闰年和几种不同的日期格式,在编写过程中,得到了phai2003网友的支持,再次表示感谢!
第一个版本
@echo off
if "%1"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
) else (
if "%2"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
)
)

set date=%1
echo %1|find "/"
if not errorlevel 1 (
call :initd %date%
goto :add
) else (
echo %date% |find "-"
if not errorlevel 1 (
call :initd %date%
goto :add
) else (
call :inith %date%
)
)

:add
if %month% LEQ 9 set month=%month:~1%
if %day% LEQ 9 set day=%day:~1%
if %month% GTR 12 (
echo illegal date &exit /b
)
set sday=%day%
call :begin
call :LBM%month% 2>nul

:return
setlocal ENABLEEXTENSIONS
if %day% EQU %sday% (
echo illegal date &exit /b
)

set year=%year: =%
set month=%month: =%
set day=%day: =%
endlocal
if %month% LEQ 9 set month=0%month%
if %day% LEQ 9 set day=0%day%
set %2=%year%%month%%day%
exit /b

setlocal ENABLEEXTENSIONS
:inith
set year=%date:~0,4%
set month=%date:~4,2%
set day=%date:~-2%
goto :EOF

:initd
for /f "tokens=1-3 delims=/- " %%i in ('echo %1') do (
set year=%%i & set month=%%j & set day=%%k
)
goto :EOF


:begin
set second_month_days=28
set /a a=%year%%%4
set /a b=%year%%%100
set /a c=%year%%%3200
set /a d=%year%%%400
if %a% EQU 0 (
if %b% NEQ 0 (
if %c% NEQ 0 (
set second_month_days=29
goto :EOF
)
)
)
if %d% EQU 0 (
if %c% NEQ 0 (
set second_month_days=29
)
)
goto :EOF


:LBM1
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF


:LBM2
if %day% LSS %second_month_days% (
set /a day+=1
) else (
if %day% EQU %second_month_days% (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM3
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM4
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM5
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM6
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM7
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM8
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM9
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM10
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM11
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF

:LBM12
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set month=1
set /a year+=1
)
)
goto :EOF

2007-11-4 16:55
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
genteman
初级用户





积分 90
发帖 43
注册 2007-10-31
状态 离线
『第 2 楼』:  

第二个版本
rem @echo off
if "%1"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
) else (
if "%2"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
)
)

set date=%1
echo %1|find "/"
if not errorlevel 1 (
call :initd %date%
goto :add
) else (
echo %date% |find "-"
if not errorlevel 1 (
call :initd %date%
goto :add
) else (
call :inith %date%
)
)

:add
if %month% LEQ 9 set month=%month:~1%
if %day% LEQ 9 set day=%day:~1%
if %month% GTR 12 (
echo illegal date &exit /b
)
set sday=%day%
call :begin
call :func

:return
setlocal
if %day% EQU %sday% (
echo illegal date &exit /b
)

set year=%year: =%
set month=%month: =%
set day=%day: =%
endlocal
if %month% LEQ 9 set month=0%month%
if %day% LEQ 9 set day=0%day%
set %2=%year%%month%%day%
exit /b

:inith
set year=%date:~0,4%
set month=%date:~4,2%
set day=%date:~-2%
goto :EOF

:initd
for /f "tokens=1-3 delims=/- " %%i in ('echo %1') do (
set year=%%i & set month=%%j & set day=%%k
)
goto :EOF


:begin
set second_month_days=28
set /a a=%year%%%4
set /a b=%year%%%100
set /a c=%year%%%3200
set /a d=%year%%%400
if %a% EQU 0 (
if %b% NEQ 0 (
if %c% NEQ 0 (
set second_month_days=29
goto :EOF
)
)
)
if %d% EQU 0 (
if %c% NEQ 0 (
set second_month_days=29
)
)
goto :EOF

:func
for %%i in (1,3,5,7,8,10) do (
if %%i EQU %month% (
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF
)
)
for %%i in (4,6,9,11) do (
if %%i EQU %month% (
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF
)
)

if %month% EQU 2 (
if %day% LSS %second_month_days% (
set /a day+=1
) else (
if %day% EQU %second_month_days% (
set day=1
set /a month+=1
)
)
goto :EOF
)

if %month% EQU 12 (
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set month=1
set /a year+=1
)
)
)

2007-11-4 16:55
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
genteman
初级用户





积分 90
发帖 43
注册 2007-10-31
状态 离线
『第 3 楼』:  

第三个版本
rem @echo off
if "%1"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
) else (
if "%2"=="" (
echo Usage:%0 [DATE] [returnstr]
exit /b
)
)

set date=%1
set date=%date:/=%
set date=%date:-=%
set year=%date:~0,4%
set month=%date:~4,2%
set day=%date:~-2%

:add
if %month% LEQ 9 set month=%month:~1%
if %day% LEQ 9 set day=%day:~1%
if %month% GTR 12 (
echo illegal date &exit /b
)
set sday=%day%
call :begin
call :func

:return
setlocal
if %day% EQU %sday% (
echo illegal date &exit /b
)

set year=%year: =%
set month=%month: =%
set day=%day: =%
endlocal
if %month% LEQ 9 set month=0%month%
if %day% LEQ 9 set day=0%day%
set %2=%year%%month%%day%
exit /b

:begin
set second_month_days=28
set /a a=%year%%%4
set /a b=%year%%%100
set /a c=%year%%%3200
set /a d=%year%%%400
if %a% EQU 0 (
if %b% NEQ 0 (
if %c% NEQ 0 (
set second_month_days=29
goto :EOF
)
)
)
if %d% EQU 0 (
if %c% NEQ 0 (
set second_month_days=29
)
)
goto :EOF

:func
for %%i in (1,3,5,7,8,10) do (
if %%i EQU %month% (
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set /a month+=1
)
)
goto :EOF
)
)
for %%i in (4,6,9,11) do (
if %%i EQU %month% (
if %day% LSS 30 (
set /a day+=1
) else (
if %day% EQU 30 (
set day=1
set /a month+=1
)
)
goto :EOF
)
)

if %month% EQU 2 (
if %day% LSS %second_month_days% (
set /a day+=1
) else (
if %day% EQU %second_month_days% (
set day=1
set /a month+=1
)
)
goto :EOF
)

if %month% EQU 12 (
if %day% LSS 31 (
set /a day+=1
) else (
if %day% EQU 31 (
set day=1
set month=1
set /a year+=1
)
)
)

2007-11-4 16:56
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
genteman
初级用户





积分 90
发帖 43
注册 2007-10-31
状态 离线
『第 4 楼』:  

这是一个统计的P,包含3个参数:文件夹路径,开始日期,结束日期,功能为统计每一天文件名部分相同的个数,文件名的格式为*20071104*。
@echo off
if "%1"=="" (
echo Usage:%0 [Directory] [StartDATE] [EndDATE]
exit /b
) else (
if "%2"=="" (
echo Usage:%0 [Directory] [StartDATE] [EndDATE]
exit /b
)
)
if "%3"=="" (
echo Usage:%0 [Directory] [StartDATE] [EndDATE]
exit /b
)


if not exist %1\nul (echo %1 is not Directory
exit /b)

:dformat
setlocal enabledelayedexpansion
set pdate=%2
set pdate=%pdate:-=%
set pdate=%pdate:/=%

set tdate=%3
set tdate=%tdate:-=%
set tdate=%tdate:/=%
call "%~dp0"dateadd.bat %tdate% tdate
:loop
set count=
if not "%pdate%"=="%tdate%" (
if exist "%1"\*%pdate%* (
for /f %%i in ('dir /b "%1"\*%pdate%*') do set /a count+=1
echo %pdate%:!count!>>"%~dp0"st.log
) else (
echo %pdate%:0>>"%~dp0"st.log
)
call "%~dp0"\dateadd.bat %pdate% pdate
goto :loop
) else (
echo done &exit /b
)
rem >>"C:\Documents and Settings\Administrator\桌面\st.log"
rem >>C:\"Documents and Settings"\Administrator\桌面\st.log

2007-11-4 17:02
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复

请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: