我是这样处理的 确定文本行数 然后就是总行数之前的日期 开始累加1 直到文本结束
似乎效率不理想
代码简化一下 但效率没上去
本想以年为递减 结果有差错 改回- @echo off
- echo 开始时间 %time%
- for /f "tokens=1-3 delims=-/\:. " %%i in ("%date%") do set /a p1=%%i,p2=100%%j%%100,p3=100%%k%%100
- set/a PN2="!(p1%%4) & !(!(p1%%100)) | !(p1%%400)",PN2+=28
- for /f "tokens=%p2%" %%i in ("31 !pN2! 31 30 31 30 31 31 30 31 30 31") do set pt=%%i
- for /f "delims=" %%i in ('type "abcd.txt"^|find /v /c ""') do set /a tt=%%i
- cd.>b.txt
- setlocal enabledelayedexpansion
- for /f "usebackq delims=" %%i in ("abcd.txt") do (
- if not defined b set b=p&call :lp
- if !p3! gtr !pt! set/a p3-=pt,p2+=1&call:next
- if !p2! lss 10 (set m2=0!p2!)else set m2=!p2!
- if !p3! lss 10 (set d3=0!p3!)else set d3=!p3!
- >>b.txt echo !p1!年!m2!月!d3!日 %%i
- if !p3! equ !pt! >>b.txt echo.
- set/a p3+=1
- )
- echo 结束时间 %time%
- pause&exit /b
- :next
- if !p2! gtr 12 set/a PN2="^!(p1%%4) & ^!(^!(p1%%100)) | ^!(p1%%400)",PN2+=28,p1+=1,p2=p2%%12
- for /f "tokens=%p2%" %%i in ("31 !pN2! 31 30 31 30 31 31 30 31 30 31") do set pt=%%i
- goto :eof
- :lp
- set/a p2-=1
- if %p2% equ 0 set/a PN2="^!(p1%%4) & ^!(^!(p1%%100)) | ^!(p1%%400)",PN2=28+PN2,p1-=1,p2=12
- for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3+=%%i&set pt=%%i
- if %p3% leq %tt% goto lp
- set/a p3-=tt
- goto :eof
复制代码
再修正一下 以年递减 不满一年 以月递减
修正一个 在行数小的情况出现的BUG
- @echo off
- echo 开始时间 %time%
- for /f "tokens=1-3 delims=-/\:. " %%i in ("%date%") do set /a p1=%%i,p2=100%%j%%100,p3=100%%k%%100
- set/a pn="!(p1%%4) & !(!(p1%%100)) | !(p1%%400)",pn+=28,pn2=pn+31
- for /f "tokens=%p2%" %%i in ("31 %pn% 31 30 31 30 31 31 30 31 30 31") do set pt=%%i
- for /f "tokens=%p2%" %%i in ("0 31 %pn2% 90 120 151 181 212 243 273 304 334") do set/a p3+=%%i
- for /f "delims=" %%i in ('type "ab cd.txt"^|find /v /c ""') do set /a tt=%%i-1
- if %tt% gtr %p3% (set/a tt-=p3,p1-=1,p2=12,p3=31)else goto lp2
- :lp
- set/a pn="!(p1%%4) & !(!(p1%%100)) | !(p1%%400)",pn+=365
- if %tt% gtr %pn% set/a tt-=pn,p1-=1&goto lp
- set/a pn="!(p1%%4) & !(!(p1%%100)) | !(p1%%400)",pn+=28
- :lp1
- if %p3% gtr %tt% goto lp2
- set/a p2-=1
- for /f "tokens=%p2%" %%i in ("31 %pn% 31 30 31 30 31 31 30 31 30 31") do set/a p3+=%%i&set pt=%%i
- if %p3% leq %tt% goto lp1
- :lp2
- set/a p3-=tt
- cd.>b.txt
- setlocal enabledelayedexpansion
- for /f "usebackq delims=" %%i in ("ab cd.txt") do (
- if !p3! gtr !pt! set/a p3-=pt,p2+=1&call:next
- if !p2! lss 10 (set m2=0!p2!)else set m2=!p2!
- if !p3! lss 10 (set d3=0!p3!)else set d3=!p3!
- >>b.txt echo !p1!年!m2!月!d3!日 %%i
- if !p3! equ !pt! >>b.txt echo.
- set/a p3+=1
- )
- echo 结束时间 %time%
- pause&exit /b
- :next
- if !p2! gtr 12 set/a pn="^!(p1%%4) & ^!(^!(p1%%100)) | ^!(p1%%400)",pn+=28,p1+=1,p2=p2%%12
- for /f "tokens=%p2%" %%i in ("31 !pn! 31 30 31 30 31 31 30 31 30 31") do set pt=%%i
- goto :eof
复制代码
[ 本帖最后由 terse 于 2008-10-20 13:48 编辑 ] |