Board logo

标题: [求助]帮助分析查找指定日期的文件代码错在哪里? [打印本页]

作者: tempuser     时间: 2008-10-29 21:32    标题: [求助]帮助分析查找指定日期的文件代码错在哪里?

代码如下:
@echo off
:finddate
cls
set  /p  fdate=请输入查询日期(例:2008-10或2008-10-18)   
set  /p  fname=请输入查询文件类型(例:.exe或.bat)   
for  %%a  in (c: d:)  do (
    for /r %%a\ %%b in ('dir /a /s /b %fname% | findstr /tc /4 %fdate%')  do (
           echo  %%b >>test.txt
           start  test.txt
    )
)
pause
goto finddate
作者: HAT     时间: 2008-10-30 00:18
把@echo off删除
看看运行结果
如果还是不明白就把结果贴出来请人帮忙
作者: terse     时间: 2008-10-30 00:30
|前加^
作者: tempuser     时间: 2008-10-30 10:03
把@echo off去掉后执行,先后显示要求输入查询日期和查询类型文件,我分别输入:
2008-10
.bat
之后就是一闪退出批处理执行的CMD窗口,实在是没有其它什么现象。

另外,我已经在  |前加了转义字符^,不行。
作者: HAT     时间: 2008-10-30 10:11
把@echo off去掉
代码保存为C:\test.bat
开始
运行
cmd
C:\test.bat
结果如何?
作者: tireless     时间: 2008-10-30 10:28
感觉干这事速度比较慢,不如用 Windows 自带的搜索。
作者: tempuser     时间: 2008-10-30 11:02
照做了,以下是测试问题:
1.CMD下提示第2个for语句不应有%%a\,它多余了,去掉它。
2.再测试,提示不应有:|,嘿!又忘加^了,加上。
3.再测试,嘿,CMD一直滚屏,不断打开记事本,发现start test.txt放在了不应该放的for循环中,把它提到for循环外。
4.在测试,没有错误提示了,但CMD窗口一直在滚动,模糊看到 echo ,应该是
echo  %%b >>test.txt发生作用,但它为什么不停啊!我执行的查找2008-10在C盘下建立的.bat文件,后来我实在没有办法,ctrl+c强行终止它,发现test.txt文本内容是很乱,根本没有我要找的.bat,我摘取其部分内容,如下:
C:\'dir
C:\/a
C:\/s
C:\/b
C:\.exe
C:\|
C:\findstr
C:\/tc
C:\/4
C:\2008-10'
C:\BIOSME\'dir
C:\BIOSME\/a
C:\BIOSME\/s
C:\BIOSME\/b
C:\BIOSME\.exe
C:\BIOSME\|
C:\BIOSME\findstr
C:\BIOSME\/tc
C:\BIOSME\/4
C:\BIOSME\2008-10'
C:\COMPAQ\'dir
C:\COMPAQ\/a
C:\COMPAQ\/s
C:\COMPAQ\/b
C:\COMPAQ\.exe
C:\COMPAQ\|
C:\COMPAQ\findstr
C:\COMPAQ\/tc
C:\COMPAQ\/4
C:\COMPAQ\2008-10'
C:\COMPAQ\AClient\'dir
C:\COMPAQ\AClient\/a
C:\COMPAQ\AClient\/s
C:\COMPAQ\AClient\/b
C:\COMPAQ\AClient\.exe
C:\COMPAQ\AClient\|
C:\COMPAQ\AClient\findstr
C:\COMPAQ\AClient\/tc
C:\COMPAQ\AClient\/4
C:\COMPAQ\AClient\2008-10'
C:\COMPAQ\Acrobat_Reader\'dir
C:\COMPAQ\Acrobat_Reader\/a
C:\COMPAQ\Acrobat_Reader\/s
C:\COMPAQ\Acrobat_Reader\/b
C:\COMPAQ\Acrobat_Reader\.exe
C:\COMPAQ\Acrobat_Reader\|
C:\COMPAQ\Acrobat_Reader\findstr
C:\COMPAQ\Acrobat_Reader\/tc
C:\COMPAQ\Acrobat_Reader\/4
C:\COMPAQ\Acrobat_Reader\2008-10'
C:\COMPAQ\Audio\'dir
C:\COMPAQ\Audio\/a
C:\COMPAQ\Audio\/s
C:\COMPAQ\Audio\/b
C:\COMPAQ\Audio\.exe
C:\COMPAQ\Audio\|
C:\COMPAQ\Audio\findstr
C:\COMPAQ\Audio\/tc
C:\COMPAQ\Audio\/4
C:\COMPAQ\Audio\2008-10'
C:\COMPAQ\Audio\Realtek\'dir
C:\COMPAQ\Audio\Realtek\/a
C:\COMPAQ\Audio\Realtek\/s
C:\COMPAQ\Audio\Realtek\/b
C:\COMPAQ\Audio\Realtek\.exe
C:\COMPAQ\Audio\Realtek\|
C:\COMPAQ\Audio\Realtek\findstr
C:\COMPAQ\Audio\Realtek\/tc
C:\COMPAQ\Audio\Realtek\/4
C:\COMPAQ\Audio\Realtek\2008-10'
C:\COMPAQ\Audio\Realtek\Config\'dir
C:\COMPAQ\Audio\Realtek\Config\/a
C:\COMPAQ\Audio\Realtek\Config\/s
C:\COMPAQ\Audio\Realtek\Config\/b
C:\COMPAQ\Audio\Realtek\Config\.exe
C:\COMPAQ\Audio\Realtek\Config\|
C:\COMPAQ\Audio\Realtek\Config\findstr
C:\COMPAQ\Audio\Realtek\Config\/tc
C:\COMPAQ\Audio\Realtek\Config\/4
C:\COMPAQ\Audio\Realtek\Config\2008-10'
C:\COMPAQ\Audio\Realtek\WDM\'dir
C:\COMPAQ\Audio\Realtek\WDM\/a
C:\COMPAQ\Audio\Realtek\WDM\/s
C:\COMPAQ\Audio\Realtek\WDM\/b
C:\COMPAQ\Audio\Realtek\WDM\.exe
C:\COMPAQ\Audio\Realtek\WDM\|
C:\COMPAQ\Audio\Realtek\WDM\findstr
C:\COMPAQ\Audio\Realtek\WDM\/tc
C:\COMPAQ\Audio\Realtek\WDM\/4
C:\COMPAQ\Audio\Realtek\WDM\2008-10'
C:\COMPAQ\chipset\'dir
C:\COMPAQ\chipset\/a
C:\COMPAQ\chipset\/s
C:\COMPAQ\chipset\/b
C:\COMPAQ\chipset\.exe
C:\COMPAQ\chipset\|
C:\COMPAQ\chipset\findstr
C:\COMPAQ\chipset\/tc
C:\COMPAQ\chipset\/4
C:\COMPAQ\chipset\2008-10'
C:\COMPAQ\DotNet\'dir
C:\COMPAQ\DotNet\/a
C:\COMPAQ\DotNet\/s
C:\COMPAQ\DotNet\/b
C:\COMPAQ\DotNet\.exe
C:\COMPAQ\DotNet\|
C:\COMPAQ\DotNet\findstr
C:\COMPAQ\DotNet\/tc
C:\COMPAQ\DotNet\/4
C:\COMPAQ\DotNet\2008-10'
C:\COMPAQ\Drivers\'dir
C:\COMPAQ\Drivers\/a

请帮我指正错误。
作者: HAT     时间: 2008-10-30 11:26    标题: Re 7楼

请养成一个好习惯:把最新的代码更新到顶楼。
否则别人想帮你,还得copy顶楼的代码再根据你每个楼层的描述修改,是不是很浪费时间呢?
作者: terse     时间: 2008-10-30 11:52
findstr /tc /4 参数问题哦
作者: tempuser     时间: 2008-10-30 14:06
怎么才能再将1楼的代码更新呢?
作者: kgdetg1127     时间: 2008-10-30 14:24
点右边的编辑,
作者: HAT     时间: 2008-10-30 14:59    标题: Re 10楼

» HAT:  登出 | 短消息 | 我的话题 | 控制面板 | 系统设置 | 命令行 | 会员 | 搜索 | 上传 | 统计 | 帮助
作者: terse     时间: 2008-10-30 15:32
改一下
@echo off
:finddate
cls
set /p fdate=请输入查询日期(例:2008-10或2008-10-18)   
set /p fname=请输入查询文件类型(例:exe或bat)   
for %%a in (c: d:) do (
    for /F "delims=" %%b in ('dir /a-d /s /tc /4 %%a\*.%fname% ^| findstr "^%fdate%"') do (
           echo  %%b >>test.txt
    )
)
start  test.txt
pause
goto finddate

作者: tempuser     时间: 2008-10-30 17:20
成功了,谢谢!
不过我还有转义字符的问题?
1.为什么要在|前加^呢?转义字符不是要取消特殊符号的功能吗?而我需要|执行特定的功能!
2.^%fdate%又是什么意思呢?用%fdate%不就可以识别这个变量了吗?如果加为什么不是^%fdate^%呢?后面的%就不需要转义了吗?
作者: tireless     时间: 2008-10-30 17:43    标题: Re 14



  Quote:
2.^%fdate%又是什么意思呢?用%fdate%不就可以识别这个变量了吗?如果加为什么不是^%fdate^%呢?后面的%就不需要转义了吗?

^ 表示在行首进行匹配。具体请看 findstr /? 的帮助。
作者: tempuser     时间: 2008-10-31 07:46
原来这里的^是表达式的一个符号不是转移字符的意思啊!慢慢理解中,谢谢!
再请教findstr的两个参数问题:
/b 和 /e分别代表"在一行的开始/结尾匹对模式"
不理解什么意思,能帮忙举个具体例子吗?
作者: tireless     时间: 2008-10-31 10:42    标题: Re 16 楼

自己多去测试。

例如 13 楼的代码:

  Quote:
for /F "delims=" %%b in ('dir /a-d /s /tc /4 %%a\*.%fname% ^| findstr "^%fdate%"') do ...

蓝色部分就可以改成 findstr /b "%fdate%"