-1

我正在开发一个程序,以在文件老化一定时间后从某个文件夹中删除文件,并通过正则表达式或扩展名匹配。我遇到了 files() 可能存在的问题

files(0) = Nothing 
files(1) = Nothing
files(2) = Nothing
ect.... 

现在它的写法,我可以放置

Else
                            log(1) = data(1)
                            log(3) = "Array field empty"
                            InsertLog(log)

file(i) = Nothing并且该程序将记录与保留一样多的文件。这将创建冗余的数据库记录并且是不想要的。有没有办法确定是否全部files(i) = Nothing,然后将代码放入其中以插入数据库?

           'If log(3) is successful that means no files were old enough or deleted successfully
           If log(3) = "Success" And IsArray(files) Then 
                For Each file In files
                    If Not file.IsNullOrEmpty(file) Then
                        'If files is actually something insert into the log
                        log(1) = file
                        InsertLog(log)
                    'could place else here 
                    End If
                Next
                files = Nothing
            Else
                'If no files or error in directory perform this
                log(1) = data(1)
                InsertLog(log)
            End If
4

3 回答 3

0

听起来你太复杂了。

  1. 创建文件时在数据库中创建时间戳
  2. 定期唤醒并检查是否需要删除任何内容
  3. 删除任何需要删除的东西

有时,当事情变得过于复杂时,也许是时候重新考虑你的攻击计划了。

于 2012-05-30T19:26:44.367 回答
0
Public Function AllArrayElementsAreNull(arr() As Object) As Boolean
    Dim FoundNonNullItem As Boolean = False
    For Each item As Object In Arr
        If item IsNot Nothing Then
            FoundNonNullItem = True
            Exit For
        End If
    Next
    Return Not FoundNonNullItem
End Function 
于 2012-05-30T19:27:37.337 回答
0

我添加了一个计数器来计算文件是否实际存在,简单的解决方案。

           If log(3) = "Success" And IsArray(files) Then
                j = 0
                For Each file In files
                    If Not file.IsNullOrEmpty(file) Then
                        log(1) = file
                        InsertLog(log)
                        j += 1
                    End If
                Next
                If j = 0 Then
                    log(3) = "Files not old enough"
                    InsertLog(log)
                End If
                files = Nothing
            Else
                log(1) = data(1)
                InsertLog(log)
            End If
于 2012-05-30T21:12:52.597 回答