【发布时间】:2017-01-24 21:54:50
【问题描述】:
我有一个 Excel 表格,上面有很多 PC 名称。每台 PC 都应在服务器上存储一个自动生成的 .zip 文件的备份。
当我运行我的代码时,它会检查 PC 名称以检查它们是否有备份。
备份过程并不完美,因此在检测到问题后可能需要手动解决问题。
我无法检测到的问题之一是备份过程是否未完成并且 .zip 文件是否损坏。
我想编写另一个函数来检测无法打开的损坏的 .zip 文件。
代码如下:
Sub check_for_all_backups()
Dim c As Range
Dim rng As Range
Dim Backup As String
For j = 1 To Worksheets.Count
Set rng = Sheets(j).UsedRange.Cells
For Each c In rng
If ispcname(Left(c, 7)) = True And Right(c, 1) = "$" Then
Dim i
i = 1
Backup = Left(c, 7)
c.Interior.ColorIndex = "0"
File = Dir(BU_Folder_Dir)
Do While File <> ""
isbig = True '|
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject") '|
myBool = False
isnew = False
Backup = Right(Backup, 6)
If InStr(File, Backup) > 0 Then
myBool = True
cfile = Dir(BU_Folder_Dir & Left(c, 7) & "*")
Do While cfile <> ""
ReDim arr(i)
arr(i) = FileDateTime(BU_Folder_Dir & cfile)
ReDim Size(i) '|
Size(i) = BU_Folder_Dir & cfile
fsize = FSO.getfile(Size(i)).Size / 1024 / 1024 'MB
If fsize <= 2048 Then 'is file smaller than 2 GB ?
isbig = False
End If '|
If Now - arr(i) < 30 Then
isnew = True
End If
i = i + 1
cfile = Dir()
Loop
If isbig = True Then '|
If c.Comment Is Nothing Then
c.AddComment ("reduce _mit size." & vbCrLf & ".zip over 2GB & (" & fsize & ")")
End If
ElseIf isbig = False Then
If Not c.Comment Is Nothing Then
c.ClearComments
End If
End If '|
If isnew = False Then
c.Interior.ColorIndex = "6"
ElseIf isnew = True Then
c.Interior.ColorIndex = "35"
End If
Exit Do
End If
File = Dir()
Loop
If Not myBool Then
c.Interior.ColorIndex = "22"
End If
End If
Next c
Next j
Call backup_statistics
End Sub
Excel 表格有更多用途,因此“$”符号仅用于区分 PC 名称和其他子/功能中的备份名称。 PC 名称由另一个名为ispcname 的函数标识。备份 .zip 文件的名称始终包含 PC 名称。
该脚本仅对文件夹和 zip 文件具有读取权限。
有大约 1000 个 zip 文件需要检查。它们的大小可以达到 2 GB,因此我需要一些方法来检查文件是否可以在不进行过多处理的情况下访问。
【问题讨论】:
-
一种选择是尝试解压缩或检查 ZIP 中的文件名 this by Ron de Bruin。或查看this question。
-
感谢您提供的信息,我将开始尝试这些方法。我已经用其他信息更新了我的问题。
标签: vba excel zip corruption