VBscript 从 0kb 大小和当前日期的目录中删除多个 .txt 文件

VBscript to delete multiple .txt files from a directory with 0kb size and with current date

如何使用 VBscript 从一个大小为 0kb 且当前日期为当前日期的目录中删除多个 .txt 文件?

如果这是一次性清理,您可能可以使用 Windows Explorer 来过滤类型、大小和日期列,以一次性找到并删除它。如果这是一个需要脚本的重复发生的事情,我的第一个猜测是做这样的事情:

Dim fs, f, f1
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("C:\FolderPath")
For Each f1 in f.Files
    If f1.Size = 0 and f1.DateLastModified = Date Then
        f.DeleteFile
    End If
Next

Set fs = Nothing
Set f = Nothing
Set f1 = Nothing

我还没有测试过这段代码,所以我会先在一个虚拟位置上试一试。希望对您有所帮助。

这是一个经过测试的版本,应该适合您;)

Option Explicit
Dim fs, f, f1, D, MyDate
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("E:\Test")
For Each f1 in f.files
    D = f1.DateLastModified
    MyDate = Split(D," ")
    If f1.size = 0 and Cdate(MyDate(LBound(MyDate))) = Date Then     
    fs.deletefile f1.path,true
End If
Next
Set fs = Nothing
Set f = Nothing
Set f1 = Nothing

删除@Hackoo 和@legendjr 代码的脂肪后(变量只使用一次(没有 f,没有机会 f.DeleteFile),伪装类型转换,cargo cult Sets to Nothing) and exploiting the fact that Dates are Doubles:

Option Explicit

Const csFolder = ".252843"

Dim dblToday : dblToday = Date()
Dim oFile
For Each oFile In CreateObject("Scripting.FileSystemObject").GetFolder(csFolder).Files
    If 0 = oFile.Size And dblToday = Fix(oFile.DateLastModified) Then oFile.Delete True
Next