我使用以下代码:
Set StorageFileSystem = CreateObject("Scripting.fileSystemObject")
Set StorageFolder = StorageFileSystem.GetFolder(PathToStorageFiles)
msgBox "Set folders for Storage"
for each Storagefile in StorageFolder.Files 'get the creation time of the oldest recording
msgBox "DateCreated: " & Storagefile.DateCreated & vbCrLf & "EarylDateTime: " & earlyDateTime & vbCrLf & "DateTime to compare: " & dateadd("h" ,-6, Now)
if Storagefile.DateCreated < dateadd("h" ,-6, Now) then
earlyDateTime = Storagefile.DateCreated
end if
next
我以前使用过这个问题,即使是在这个程序中也是如此。但是这次它似乎永远不会做任何事情。该文件夹中有超过130,000个文件(391GB)。我不知道是否应该包含一个延迟,以便程序可以枚举它们,或者是否有其他问题我只是看不到。 有任何想法吗?我正在使用VBS,2个set语句和for循环之间的msgBox工作,但是for循环的开头和if语句之间没有。
答案 0 :(得分:1)
你是说For循环中的代码似乎不起作用?如果文件夹中没有任何文件,似乎无效。因此,请检查PathToStorageFiles
。
获取最早的录制创建时间的逻辑是有缺陷的 - 在Now
被视为最旧并且设置为earlyDateTime之前6小时的任何时间。
请尝试以下代码,并提供示例输出:
PathToStorageFiles = "C:\Test" ' <=- Change this!
Set StorageFileSystem = CreateObject("Scripting.fileSystemObject")
Set StorageFolder = StorageFileSystem.GetFolder(PathToStorageFiles)
sOldestFile = "" ' Stores the full name of the file
earlyDateTime = dateadd("h" ,-6, Now) ' Assuming 6 hours before script started is oldest (it can be just Now)
wscript.echo StorageFolder.Files.Count & " files in the folder " & PathToStorageFiles
for each Storagefile in StorageFolder.Files 'get the creation time of the oldest recording
if Storagefile.DateCreated < earlyDateTime then
sOldestFile = Storagefile.Path
earlyDateTime = Storagefile.DateCreated
wscript.echo "earlyDateTime changed to " & earlyDateTime & " | " & sOldestFile
end if
next
wscript.echo vbCrLf & "Oldest file: " & sOldestFile & vbCrLf & "Created on: " & earlyDateTime
在旁注中,您应该修改它以处理子文件夹,然后将文件移动到文件夹中。单个文件夹中的130,000个文件很乱!
<小时/> 的更新强>
根据您发布的解决方案,您可以进行改进。
首先,使用1个FileSystemObject。
然后是for循环中的recentFile
。您应该首先将其设置为零,而不是2次比较。话虽如此,你有机会计算时差。
recentFile = 0
For Each file in colFiles
If file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
最后,如果服务器上的D:是NAS,那么您可以将代码拆分为两部分 - 一部分搜索最近,另一部分搜索最旧。然后使用批处理文件start cscript.exe //nologo <script#.vbs>
方法在2个进程中启动它们。这需要2个txt文件进行输出。
如果只有一个文件夹可以获得最新的&amp;最旧的文件,它可以在1 for for循环中。
答案 1 :(得分:0)
这是我开始工作的代码:
Option Explicit
Dim LocalStorage, NewLocalStorage, recentFile, objFSO, colFiles, objFolder, file, OldestDate, strOldestDate, fso, ts, objFile
LocalStorage = "D:\BlueIris\Storage"
NewLocalStorage = "D:\BlueIris\New"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewLocalStorage)
Set colFiles = objFolder.Files
For Each file in colFiles
If recentFile = "" Then
recentFile = file.DateCreated
ElseIf file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
Set objFolder = objFSO.GetFolder(LocalStorage)
Set colFiles = objFolder.Files
OldestDate = Now
For Each objFile in colFiles
if objFile.DateCreated < OldestDate Then
OldestDate = objFile.DateCreated
strOldestDate = objFile.DateCreated
End if
Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("C:\DVRInfo.txt", true)
ts.writeline recentFile
ts.writeline strOldestDate
ts.close
我在实际服务器上运行它,以便它比我尝试的原始代码运行得快得多。如果您仍然存在缺陷,请告诉我,我希望尽可能高效。
由于
编辑: 新代码:
Option Explicit
Dim LocalStorage, NewLocalStorage, recentFile, objFSO, colFiles, objFolder, file, OldestDate, strOldestDate, fso, ts, objFile
LocalStorage = "D:\BlueIris\Storage"
NewLocalStorage = "D:\BlueIris\New"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewLocalStorage)
Set colFiles = objFolder.Files
Set recentFile = 0
For Each file in colFiles
If file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
Set objFolder = objFSO.GetFolder(LocalStorage)
Set colFiles = objFolder.Files
OldestDate = Now
For Each objFile in colFiles
if objFile.DateCreated < OldestDate Then
OldestDate = objFile.DateCreated
strOldestDate = objFile.DateCreated
End if
Next
Set ts = fso.CreateTextFile ("C:\DVRInfo.txt", true)
ts.writeline recentFile
ts.writeline strOldestDate
ts.close