找回密码
 加入
搜索
查看: 4100|回复: 13

[AU3基础] 【已解决】如何找出一个文件中日期最近的文件

 火.. [复制链接]
发表于 2011-12-18 22:25:59 | 显示全部楼层 |阅读模式
本帖最后由 lion.lee 于 2011-12-28 14:47 编辑

想了很多办法尝试找出一个文件中日期最近的文件,但以下这段代码可靠性实在太差,请各位大大帮忙看看如何修改。多谢!
#include <date.au3>
#include <math.au3>
MsgBox(0,"该文件夹里最近的文件创建时间数值:",FindAllFile("e:"))

Func FindAllFile($sDir)
        Dim $max
        Local $hSearch = FileFindFirstFile($sDir & "\*.*")
        ; 检查搜索是否成功
        If $hSearch = -1 Then Return

        While 1
                Local $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop

                If @extended Then
                        FindAllFile($sDir & "" & $sFile)
                        ContinueLoop
                EndIf
                $time = FileGetTime($sDir & "" & $sFile, 1, 1)
                $valve = _DateToDayValue(StringLeft($time, 4), StringMid($time, 5, 2), StringMid($time, 7, 2))
                $max = _Max($valve, $max)
        WEnd
        ; 关闭搜索句柄
        FileClose($hSearch)
        Return $max
EndFunc   ;==>FindAllFile
发表于 2011-12-18 23:08:07 | 显示全部楼层
全盘搜索,对于Au3来说,肯定不是强项,建议使用Dos的 Dir 命令,扫描出结果文档后利用正则及数组比对处理
发表于 2011-12-18 23:37:08 | 显示全部楼层
想了很多办法尝试找出一个文件中日期最近的文件,但以下这段代码可靠性实在太差,请各位大大帮忙看看如何修 ...
lion.lee 发表于 2011-12-18 22:25



    你所谓的“可靠性实在太差”具体指啥?

怎么个不可靠了?

若是速度问题,你别用递归,用迭代试试,看看效果是不是好点!

不过,象这种线性问题,效率最高的就是多线程。并且这个问题,一点线程之间的干扰都没有。也基本没有线程阻塞,au3提供的多进程函数都太笨重了,连启动都要花些时间。
发表于 2011-12-19 08:19:12 | 显示全部楼层
版主说的是,我也是这样遍历的。 dir /o d
 楼主| 发表于 2011-12-19 09:06:16 | 显示全部楼层
“可靠性差”是指有的时候能运算出结果,有的时候返回空值。不能保证每次都能得到想要的结果。
 楼主| 发表于 2011-12-19 09:10:27 | 显示全部楼层
多谢各位的指点,现在就去试试dir /od。
发表于 2011-12-19 10:18:25 | 显示全部楼层
本帖最后由 Ycxw2008 于 2011-12-19 10:21 编辑

也是论坛别人帮我的,发给你参考下
#Include <File.au3>
#Include <Array.au3>
$Dir = "C:"
$FileList = _FileListToArray($Dir, "*.log", 1)
$t = 0
$f = 0
For $i = 1 To UBound($FileList)-1
        If Number(FileGetTime($Dir & $FileList[$i], 1,1)) > $t Then
                $t = Number(FileGetTime($Dir & $FileList[$i], 1,1))
                $f = $i
                EndIf
Next
;MsgBox(0,0,$FileList[$f])
ShellExecute($Dir&$FileList[$f])

评分

参与人数 1金钱 +10 收起 理由
lnlyf + 10

查看全部评分

 楼主| 发表于 2011-12-20 21:46:35 | 显示全部楼层
本帖最后由 lion.lee 于 2011-12-21 14:40 编辑

我准备用这个代码测试一下能不用稳定运行。
#include <file.au3>
#include <date.au3>
_filegetTime("F:\soft")
Func _filegetTime($path)
        RunWait(@ComSpec & " /c " & "dir " & $path & " /tc >" & @ScriptDir & "\dirresult.txt", "", @SW_HIDE)
              ;将dir /od 修改为 dir /tc
        $str = FileRead("dirresult.txt")
        $str = StringReplace($str, " ", "")
        FileDelete("dirresult.txt")
        FileWrite("filetime.txt", $str)
        Dim $array, $string, $timestr, $filename
        _FileReadToArray("filetime.txt", $array)
        FileDelete("filetime.txt")
        For $i = 1 To $array[0]
                If StringInStr($array[$i], "<DIR>") And Not StringInStr($array[$i], ".") Then
                        $string = StringSplit($array[$i], "<DIR>", 1)
                        $timestr = $string[1]
                        $filename = $string[2]
                        $timevalve = _DateToDayValue(StringLeft($timestr, 4), StringMid($timestr, 6, 2), StringMid($timestr, 9, 2))
                        $now = _DateToDayValue(@YEAR, @MON, @MDAY)
                        $tonow = $now - $timevalve
                        If $tonow > 5 Then
                                MsgBox(0, 0, $filename & "可以删除")
                                ;DirRemove($path & "" & $filename, 1)
                                                               ;路径忘记加上了,本次编辑补上
                        EndIf
                EndIf
        Next
EndFunc   ;==>_filegetTime
发表于 2011-12-20 22:14:30 | 显示全部楼层
我准备用这个代码测试一下能不用稳定运行。
lion.lee 发表于 2011-12-20 21:46



    你这个是找多个文件?并非“日期最近的文件”吧,且没有遍历子目录
发表于 2011-12-20 22:38:41 | 显示全部楼层
dir那个日期好像是文件修改日期,不适用于某些场合
发表于 2011-12-20 22:41:11 | 显示全部楼层
用这个代码做测试,发现递归还没有完成时,就打印了?
很奇怪,如下面代码,还在递归着呢,就执行到MsgBox语句了




Local $iInit = TimerInit()
FindAllFile("c:")
Local $iTimer = TimerDiff($iInit)

MsgBox(0, "该文件夹用时:", $iTimer)

Func FindAllFile($sDir)
        Dim $max
        Local $hSearch = FileFindFirstFile($sDir & "\*.*")
        ; 检查搜索是否成功
        If $hSearch = -1 Then Return

        While 1
                Local $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop

                If @extended Then
                        FindAllFile($sDir & "\" & $sFile)
                Else
                        ConsoleWrite($sDir & "\" & $sFile & @CRLF)
                EndIf
        WEnd
        ; 关闭搜索句柄
        FileClose($hSearch)
EndFunc   ;==>FindAllFile
发表于 2011-12-21 08:39:20 | 显示全部楼层
学习。。。。
发表于 2011-12-25 14:15:52 | 显示全部楼层
学习了,谢谢
 楼主| 发表于 2011-12-28 14:47:23 | 显示全部楼层
写这些代码是为了自动删除之前备份的文件,因备份文件夹的目录都是新创建的,所以用dir /tc可以满足我的要求,不需要遍历了。该问题已经解决,多谢前辈们的指点。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-1 03:30 , Processed in 0.082127 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表