标题: [求助]抓取文件内容,有点小复杂
[打印本页]
作者: acmilanxr
时间: 2010-10-25 10:00
标题: [求助]抓取文件内容,有点小复杂
文本文件,内容格式如下:
2010-10-20 16:02:20 122.400000
2010-10-20 16:02:25 122.400000
2010-10-20 16:02:26 122.400000
2010-10-20 16:02:27 122.400000
2010-10-20 16:02:28 122.400000
2010-10-20 16:02:29 122.400000
2010-10-20 16:02:30 122.400000
2010-10-20 16:02:31 0.000000
2010-10-20 16:02:32 122.300000
2010-10-20 16:02:33 122.200000
2010-10-20 16:02:34 122.300000
2010-10-20 16:02:35 122.200000
2010-10-20 16:02:36 122.300000
2010-10-20 16:02:37 122.200000
2010-10-20 16:02:38 122.300000
2010-10-20 16:02:39 122.200000
2010-10-21 08:31:17 122.300000
2010-10-21 08:31:22 0.000000
2010-10-21 08:31:27 122.300000
2010-10-21 08:31:32 122.300000
2010-10-21 08:31:37 122.300000
2010-10-21 08:31:42 122.300000
2010-10-21 08:31:47 122.300000
2010-10-21 08:31:52 122.400000
2010-10-21 08:31:57 122.300000
2010-10-21 08:32:02 122.400000
2010-10-21 08:32:07 122.300000
2010-10-21 08:32:12 122.400000
2010-10-21 08:32:17 122.200000
2010-10-21 08:32:22 122.300000
2010-10-21 08:32:27 122.200000
2010-10-21 08:32:32 122.300000
2010-10-21 08:32:37 122.400000
2010-10-21 08:32:42 122.300000
2010-10-22 16:02:32 122.300000
2010-10-22 16:02:33 122.200000
2010-10-22 16:02:34 122.300000
2010-10-22 16:02:35 122.200000
2010-10-22 16:02:36 122.300000
2010-10-22 16:02:37 122.200000
2010-10-22 16:02:38 122.300000
2010-10-22 16:02:39 122.200000
2010-10-22 08:31:17 122.300000
2010-10-23 08:31:42 122.300000
2010-10-23 08:31:47 122.300000
2010-10-23 08:31:52 122.400000
2010-10-23 08:31:57 122.300000
2010-10-23 08:32:02 122.400000
2010-10-23 08:32:07 122.300000
2010-10-23 08:32:12 122.400000
2010-10-23 08:32:17 122.200000
2010-10-24 08:31:42 122.300000
2010-10-24 08:31:47 122.300000
2010-10-24 08:31:52 122.400000
2010-10-24 08:31:57 122.300000
2010-10-24 08:32:02 122.400000
2010-10-24 08:32:07 122.300000
2010-10-24 08:32:12 122.400000
2010-10-25 08:31:37 122.300000
2010-10-25 08:31:42 122.300000
2010-10-25 08:31:47 122.300000
2010-10-25 08:31:52 122.400000
2010-10-25 08:31:57 122.300000
2010-10-25 08:32:02 122.400000
要求:根据判断时间点,把一周之内的文本内所有内容抓取出来,比如从2010年10月18日抓到2010年10月24日,抓出来的数据送到一个新的文本文件里,新的文本文件根据生成日期命名,比如叫20101024.txt。
因为这个文本文件,内容是不断增加,每天的数据都会不断的写在里面,有几个问题,一是判断一周这个时间段,从周一抓到周日,这个怎么判断哪天是周一,哪天是周日;二是生成出来的文件名称根据每次生成的日期自动命名,这个又是怎么写。
本人这方面小白,请高手指教,谢谢了

作者: clinttt
时间: 2010-10-25 14:32
这个很简单啊!
作者: acmilanxr
时间: 2010-10-25 15:13
那是怎么写呢,我对这个基本不了解。。
作者: a113432
时间: 2010-10-25 16:22
不太懂 帮你顶上去
作者: clinttt
时间: 2010-10-25 18:31
日期格式是固定的吗?
是2010-02-01 还是 2010-2-1的格式?
作者: clinttt
时间: 2010-10-25 19:25
文件里只有一周吗?如果有很多周,你想得到什么?只得到一周的内容?还是按周输出。你说清楚点啊!
作者: clinttt
时间: 2010-10-25 19:27
echo 现在开始统计: >c:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (c:\123.txt) do (
if %%c GEQ 2010-10-24 if %%c LSS 2010-10-25 (echo %%c %%d %%e >>c:\%date:~0,10%.txt))
作者: acmilanxr
时间: 2010-10-26 09:23
Quote: |
Originally posted by clinttt at 2010-10-25 19:25:
文件里只有一周吗?如果有很多周,你想得到什么?只得到一周的内容?还是按周输出。你说清楚点啊! |
|
这个文件我只是举个例子,正式的话里面是所有日期的数据都在里面,输出是根据文件内容中的日期判断,抓取从周一到周日的数据,一周抓一次,比如第一次从2010年10月18日抓到24日,下次运行就从2010年10月25日抓到31日,依此类推,(一周抓一次,这个和脚本无关,我可以在计划任务中定义)日期格式随便,可以是2010-02-01,也可以是2010-2-1。
[
Last edited by acmilanxr on 2010-10-26 at 10:18 ]
作者: acmilanxr
时间: 2010-10-26 10:23
Quote: |
Originally posted by clinttt at 2010-10-25 19:27:
echo 现在开始统计: >c:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (c:\123.txt) do (
if %%c GEQ 2010-10-24 if %%c LSS 2010-10-25 (echo %%c %%d %%e >>c:\%date:~ ... |
|
这个再帮我改下吧,根据内容判断抓周一到周日的内容,还有就是我运行你这个脚本,输出文件没有生成。。。
作者: clinttt
时间: 2010-10-26 14:05
没有生成是因为你没有按脚本准备文件,以及需要知道输入文件的路径。
作者: acmilanxr
时间: 2010-10-26 14:09
Quote: |
Originally posted by clinttt at 2010-10-26 14:05:
没有生成是因为你没有按脚本准备文件,以及需要知道输入文件的路径。 |
|
现在可以了,但关于从文件内容中判断日期,这个怎么写,比如2010年10月18日是周一,24日是周日,25日又是周一,怎么判断呢?
作者: clinttt
时间: 2010-10-26 14:51
@Echo Off
set date0=2017-07-26
set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~5,2% LEQ 2 (set /a remo=%date0:~5,2%/2+30*%month1%) else (
if %date0:~5,2% LEQ 8 (if %year1%==0 (set /a remo=%date0:~5,2%/2+30*%month1%-1) else (
set /a remo=%date0:~5,2%/2+30*%month1%-2)) else (
if %year1%==0 (set /a remo=%month2%/2+30*%month1%-1) else (
set /a remo=%month2%/2+30*%month1%-2)))
set year1=
set month1=
set month2=
set /a reda=%date0:~-2%-1
set date0=
set /a week1=(%reye%+%remo%+%reda%+4)%%7
set reye=
set remo=
set reda=
if %week1%==1 set week0=星期一
if %week1%==2 set week0=星期二
if %week1%==3 set week0=星期三
if %week1%==4 set week0=星期四
if %week1%==5 set week0=星期五
if %week1%==6 set week0=星期六
if %week1%==0 set week0=星期日
set week1=
echo %week0%
set week0=
作者: acmilanxr
时间: 2010-10-26 15:23
Quote: |
Originally posted by clinttt at 2010-10-26 14:51:
@Echo Off
set date0=2017-07-26
set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~ ... |
|
连在一起怎么弄。。。。两个脚本拼在一起,从哪里开始插入?
echo 现在开始统计:> C:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (C:\OV-BLM-001_2010-10-20_16-02-20.txt) do (if %%c GEQ 2010-10-18 if %%c LEQ 2010-10-24 (echo %%c %%d %%e >> C:\%date:~0,10%.txt))
@Echo Off
set date0=2010-10-26
set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~5,2% LEQ 2 (set /a remo=%date0:~5,2%/2+30*%month1%) else (
if %date0:~5,2% LEQ 8 (if %year1%==0 (set /a remo=%date0:~5,2%/2+30*%month1%-1) else (
set /a remo=%date0:~5,2%/2+30*%month1%-2)) else (
if %year1%==0 (set /a remo=%month2%/2+30*%month1%-1) else (
set /a remo=%month2%/2+30*%month1%-2)))
set year1=
set month1=
set month2=
set /a reda=%date0:~-2%-1
set date0=
set /a week1=(%reye%+%remo%+%reda%+4)%%7
set reye=
set remo=
set reda=
if %week1%==1 set week0=星期一
if %week1%==2 set week0=星期二
if %week1%==3 set week0=星期三
if %week1%==4 set week0=星期四
if %week1%==5 set week0=星期五
if %week1%==6 set week0=星期六
if %week1%==0 set week0=星期日
set week1=
echo %week0%
set week0=
作者: clinttt
时间: 2010-10-26 16:08
还没明白你的意思?
是不是这样的?你周三运行脚本,你就想输出本周日到周六的内容?
只输出本周还是上周的?
作者: acmilanxr
时间: 2010-10-26 16:25
Quote: |
Originally posted by clinttt at 2010-10-26 16:08:
还没明白你的意思?
是不是这样的?你周三运行脚本,你就想输出本周日到周六的内容?
只输出本周还是上周的? |
|
哦,是这样,这个批处理脚本会每周日运行一遍,把那个文本文件中的内容,根据判断日期,就是2010-10-25这一列,抓取其中本周一至本周日的数据,送到另一个新的文本文件中,比如这周日运行,抓取这周的所有数据,下周的周日运行,就抓取下周的所有数据,依此类推,文件名通过运行当天的日期命名。
作者: acmilanxr
时间: 2010-10-27 10:20
人呢,要怎么弄?
作者: dguest
时间: 2010-10-27 10:39
你可以这样考虑:
1、每周一抓取上周的数据,这样要比每周日抓取一次本周的数据更准确些,因为一周抓取一次,一周7天位固定的,可以一个周一的日期当前日期减7,就计算出上周一的日期来了,减1就是上周日的日期。
2、输出到当前日期的文本中,可以直接 >> %date:~0,10%.txt。
作者: acmilanxr
时间: 2010-10-27 10:47
Quote: |
Originally posted by dguest at 2010-10-27 10:39:
你可以这样考虑:
1、每周一抓取上周的数据,这样要比每周日抓取一次本周的数据更准确些,因为一周抓取一次,一周7天位固定的,可以一个周一的 ... |
|
怎么算倒是小事,现在就是这两个内容怎么合并在一起,我看了下,似乎是week0和%%c之间要有个链接,但互相之间怎么赋值我不是很了解,这两个都是变量?用set连么?
作者: clinttt
时间: 2010-10-27 16:38
@Echo Off
set date0=%date:~0,10%
set /a year0 = %date0:~0,4%
if %date0:~4,1% == %date0:~6,1% (set month0=%date0:~5,1%) else (
set month0=%date0:~5,2%)
if %month0:~,1% == 0 set /a month0 = %month0:~-1%
if %date0:~4,1% == %date0:~-2,1% (set day0=%date0:~-1%) else (
set day0=%date0:~-2%)
if %day0:~,1% == 0 set /a day0 = %day0:~-1%
set date0=
set /a year1=%year0%%%4
set /a month1=%month0%-1
set /a month2=%month0%+1
set /a reye=(%year0%-1970+1)/4+365*(%year0%-1970)
set year0=
if %month0% LEQ 2 (set /a remo=%month0%/2+30*%month1%) else (
if %month0% LEQ 8 (
if %year1%==0 (set /a remo=%month0%/2+30*%month1%-1) else (
set /a remo=%month0%/2+30*%month1%-2)) else (
if %year1%==0 (set /a remo=%month2%/2+30*%month1%-1) else (
set /a remo=%month2%/2+30*%month1%-2)))
set year1=
set month1=
set month2=
set month0=
set /a reda=%day0%-1
set day0=
set /a re = %reye%+%remo%+%reda%-6
set /a week0 = (%reye%+%remo%+%reda%+4)%%7
if %week0% NEQ 0 echo 今天不是星期天,很抱歉!请星期天再运行! & goto end
set week0=
set reye=
set remo=
set reda=
set /a year1=(%re%%%1461)/365
set /a day0=(%re%%%1461)%%365
if %year1%==3 if %day0%==0 (set /a year1=%year1%-1 & set /a day0=365) else (set /a day0=%day0%-1)
if %year1%==4 set /a year1=%year1%-1 & set /a day0=364
set /a year0=1970+(%re%/1461)*4+%year1%
set re=
set /a month0=(%day0%)/31+1
set /a day0=%day0%%%31+1
set /a month1=%month0%-1
set /a month2=%month1%-1
if %month0% EQU 2 (if %year1%==2 (if %day0% GEQ 30 (set /a month0 = 3 & set /a day0=%day0%-29)) else (
if %day0% GEQ 29 (set /a month0 = 3 & set /a day0=%day0%-28))) else (
if %month0% GEQ 9 (if %year1%==2 (set /a day0=%day0%+%month2%/2+1) else (set /a day0=%day0%+%month2%/2+2)) else (
if %month0% GEQ 3 (if %year1%==2 (set /a day0=%day0%+%month1%/2+1) else (set /a day0=%day0%+%month1%/2+2))))
set year1=
set month1=
set month2=
set /a month3=%month0%%%2
if %month0% GTR 7 if %month3%==0 (if %day0% GEQ 32 (set /a month0=%month0%+1 & set /a day0=%day0%-31)) else (
if %day0% GEQ 31 (set /a month0=%month0%+1 & set /a day0=%day0%-30))
if %month0% GEQ 3 if %month0% LEQ 7 if %month3%==0 (if %day0% GEQ 31 (set /a month0=%month0%+1 & set /a day0=%day0%-30)) else (
if %day0% GEQ 32 (set /a month0=%month0%+1 & set /a day0=%day0%-31))
set month3=
if %month0% == %month0:~,1% set month0=0%month0%
if %day0% == %day0:~,1% set day0=0%day0%
set date0=%year0%-%month0%-%day0%
set year0=
set month0=
set day0=
echo 现在开始统计: >c:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (C:\OV-BLM-001_2010-10-20_16-02-20.txt) do (
if %%c GEQ %date0% if %%c LSS %date:~0,10% (echo %%c %%d %%e >>c:\%date:~0,10%.txt) & set a=a)
if defined a echo 已统计 %date0%(周一) 到 %date:~0,10%(周日) 的数据。 & set a=
set date0=
:end
pause
作者: acmilanxr
时间: 2010-11-3 16:30
感谢clinttt的大力帮助, 还有个问题想请教下, 可不可以同时对多个txt文件进行统计, 比如这句话: for /f "tokens=1,2,3" %%c in (C:\OV-BLM-001_2010-10-20_16-02-20.txt) , 如果我想同时抓取两个或两个以上文件的内容,该怎么修改呢?
作者: clinttt
时间: 2010-11-4 13:56
你将文件名加到 for语句的 in 后面的括号里,看是不是你想要的效果!
作者: clinttt
时间: 2010-11-4 13:57
给点分吧!
作者: acmilanxr
时间: 2010-11-4 17:31
Quote: |
Originally posted by clinttt at 2010-11-4 13:56:
你将文件名加到 for语句的 in 后面的括号里,看是不是你想要的效果! |
|
不是,我意思是有大批量的那种,有没有类似 D:\路径\*.txt 这样的写法,这样就可以不用一个个列举出来了。
BTW:为啥我只能加一分
作者: clinttt
时间: 2010-11-4 17:47
可以使用通配符,就按你的写法就可以!
作者: acmilanxr
时间: 2010-11-5 09:31
Quote: |
Originally posted by clinttt at 2010-11-4 17:47:
可以使用通配符,就按你的写法就可以! |
|
不行啊,试过了,生成的文件里面没有相关的数据存在
作者: clinttt
时间: 2010-11-5 10:53
for %%d in (c:\*.txt) do (
if %%~nd NEQ %date:~0,10% (
for /f "tokens=1,2,3" %%c in (%%d) do (
if %%c GEQ %date0% if %%c LSS %date:~0,10% (echo %%c %%d %%e >>c:\%date:~0,10%.txt) & set a=a)))
作者: yf6122010
时间: 2010-12-9 10:37
学习了!