我是否需要等待时间来设置新的文件夹vbs?(Do I need a wait time for setting a new folder vbs?)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
我是否需要等待时间来设置新的文件夹vbs?(Do I need a wait time for setting a new folder vbs?)

我使用以下代码:

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 next

I 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

cscript输出

另外,您应该修改它以处理子文件夹,然后将文件移动到文件夹中。 单个文件夹中的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.close

I 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

更多推荐

本文发布于:2023-04-16 14:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/22a13c3823858f646a3d4db3326b5610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   时间   vbs   wait   setting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!