数组为空,如何验证

时间:2012-05-30 19:09:04

标签: arrays vb.net ssis filesystems

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

的问题
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

3 个答案:

答案 0 :(得分:0)

听起来你太复杂了。

  1. 创建文件时在数据库中创建时间戳
  2. 定期唤醒并检查是否需要删除任何内容
  3. 删除任何需要删除的内容
  4. 有时,当事情变得太复杂时,也许是时候重新考虑你的攻击计划了。

答案 1 :(得分: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 

答案 2 :(得分: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