简体   繁体   中英

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

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

After removing the fat of @Hackoo's and @legendjr code (variables used just once (no f, no chance of f.DeleteFile), camouflaged type conversions, cargo cult Sets to Nothing ) and exploiting the fact that Dates are Doubles :

Option Explicit

Const csFolder = ".\30252843"

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

This a tested version and it should works for you ;)

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

If this is a one-time cleanup, you could probably use Windows Explorer to filter the type, size, and date columns to find and delete it in one go. If this is a recurring thing that needs a script, my first guess would be to do something like this:

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

I haven't tested this code yet, so I would try it out on a dummy location first. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM