我使用以下代码:
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语句之间没有。
I am using the following code:
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 nextI have used this before without problem, even in the program that this is in. However this time it never seems to do anything. The folder has over 130,000 files in it (391GB). I don't know if I should include a delay so that the program can emumerate them or if there is some other problem that I just don't see. Any ideas? I'm using VBS, the msgBox between the 2 set statements and the for loop works, but the one between the opening of the for loop and the if statement does not.
最满意答案
你是说For循环中的代码似乎不起作用? 如果文件夹中没有任何文件,似乎无效。 因此,请检查PathToStorageFiles的值。
获得最早录制创建时间的逻辑是有缺陷的 - 任何时间都是6小时之前, Now被视为最旧并设置为earlyDateTime。
请尝试以下代码,并提供示例输出:
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个文件很乱!
UPDATE
根据您发布的解决方案,您可以进行改进。
首先,使用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文件进行输出。
如果只有一个文件夹来获取最新和最旧的文件,则它可以是1 for for循环。
This is the code that I got to work:
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.closeI run this on the actual server so that it runs a lot faster than the original code I attempted. Let me know if you still flaws in this please, I want to be as efficient as possible.
Thanks
EDIT: New code:
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更多推荐
发布评论