strOldestFile
似乎是在strOldestFile = ""
聲明中初始化的零長度字符串。
Size Property
For files, returns the size, in bytes, of the specified file. For folders, returns the size, in bytes, of all files and subfolders contained in the folder.
object.Size
The object
is always a File or Folder object
你可以在子文件夾或者迭代文件或結束循環如下:
If strOldestFile = "" Then
Wscript.Echo "No file to delete"
intFolderSize = 0 ' end loop
' or recurse to subfolders
' or choose another strategy at all
Else
objFSO.DeleteFile(strOldestFile)
intFolderSize = Int(((objFolder.Size/1024)/1024)/1024)
End If
下一個腳本應該工作:
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim objFSO, strOldestFile, dtmOldestDate, strFolder, oFolder, intFolderSize
Set objFSO = CreateObject("Scripting.FileSystemObject")
dtmOldestDate = Now
strFolder = "C:\Users\PLEX\Downloads\Completed\"
strOldestFile = ""
Set oFolder = objFSO.GetFolder(strFolder)
intFolderSize = Int(((oFolder.Size/1024)/1024)/1024)
'Do While intFolderSize >= 79
strOldestFile = ""
dtmOldestDate = Now
FindOldestFile oFolder
'objFSO.DeleteFile(strOldestFile)
strResult = strResult & vbNewLine & dtmOldestDate & vbTab & strOldestFile
intFolderSize = Int(((oFolder.Size/1024)/1024)/1024)
'Loop
Wscript.Echo strResult
Wscript.Quit
Sub FindOldestFile(objFolder)
Dim objFile, colFiles, colFolders, strFile, dtmFileDate
' find oldest file
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFile = objFile.Path
dtmFileDate = objFile.DateCreated
If dtmFileDate < dtmOldestDate Then
dtmOldestDate = dtmFileDate
strOldestFile = strFile
End If
Next
' recurse subfolders
Set colFolders = objFolder.SubFolders
For Each objFile in colFolders
FindOldestFile objFile
Next
End Sub
注意,有評論了調試一些命令目的(Do While
,Loop
,DeleteFile
);最舊的文件是echo
而不是。
上面的代碼沒有任何意義上的優化;也許更好的方式是,以避免重讀文件系統重複屬性:
- 創建的文件路徑和文件日期和大小的數組,
- 排序順序,排列按日期,然後
- 刪除文件總結刪除大小...
編輯:完整的代碼基本錯誤處理
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim objFSO, dtmOldestDate, strFolder, oFolder, intFolderSize
Dim ii, intSizeToDele, arrAux, arrFilesSorted, arrFilesUnSort()
ii = -1
strFolder = "d:\test" ' "C:\Users\PLEX\Downloads\Completed\"
dtmOldestDate = Now
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = objFSO.GetFolder(strFolder)
FindAllFiles oFolder
arrFilesSorted = BubbleSort(arrFilesUnSort,"")
intFolderSize = oFolder.Size
intSizeToDele = Int(intFolderSize/50) ' redefine to match your need
strResult = strResult & vbTab & ii _
& vbTab & intSizeToDele & vbTab & intFolderSize
For ii = 0 To UBound(arrFilesSorted)
arrAux = Split(arrFilesSorted(ii), "|")
strResult = strResult & vbNewLine & ii _
& vbTab & arrAux(0) _
& vbTab & arrAux(1) _
& vbTab & arrAux(2)
On Error Resume Next ' basic error handling - start
''''' objFSO.DeleteFile(arrAux(2)) ' uncomment no sooner than debugged
If Err.Number = 0 Then
Else
strResult = strResult & vbNewLine & ii _
& vbTab & CStr(Err.Number) & " (0x" & Hex(Err.Number) & ") " & Err.Description
End If
On Error GoTo 0 ' basic error handling - end
intSizeToDele = intSizeToDele - arrAux(1)
If intSizeToDele <= 0 Then Exit For
Next
strResult = strResult & vbNewLine & ii _
& vbTab & intSizeToDele & vbTab & intFolderSize
Wscript.Echo strResult
Wscript.Quit
Sub FindAllFiles(objFolder)
Dim objFile, colFiles, colFolders
' find all files
Set colFiles = objFolder.Files
For Each objFile in colFiles
ii = ii + 1
ReDim Preserve arrFilesUnSort(ii)
arrFilesUnSort(ii) = timeStamp(objFile.DateCreated) & "|" & objFile.Size & "|" & objFile.Path
Next
' recurse subfolders
Set colFolders = objFolder.SubFolders
For Each objFile in colFolders
FindAllFiles objFile
Next
End Sub
Function BubbleSort(ByVal arrData,strSort)
'Based on http://vbscripter.blogspot.cz/2008/03/q-how-do-i-sort-data-in-array.html
'Input: arrData = Array of data. Text or numbers.
'Input: strSort = Sort direction (ASC or ascending or DESC for descending)
'Output: Array
'
Dim i, j, TempValue
If Not Trim(UCase(strSort)) = "DESC" Then strSort = "ASC"
If strSort = "ASC" Then
For i = 0 to UBound(arrData)
For j = 0 to UBound(arrData) - 1
If arrData(j) > arrData(j+1) Then
TempValue = arrData(j+1)
arrData(j+1) = arrData(j)
arrData(j) = TempValue
End If
Next
Next
Else
For i = 0 to UBound(arrData)
For j = 0 to UBound(arrData) - 1
If arrData(j) < arrData(j+1) Then
TempValue = arrData(j+1)
arrData(j+1) = arrData(j)
arrData(j) = TempValue
End If
Next
Next
End If
BubbleSort = arrData
End Function
Function timeStamp(tt)
'Input: tt = date (a Variant of subtype Date)
'Output: sortable, fixed-length string: date formatted as yyyymmddHHMMSS
timeStamp = CStr(Year (tt) & _
Right("0" & Month (tt),2) & _
Right("0" & Day (tt),2) & _
Right("0" & Hour (tt),2) & _
Right("0" & Minute(tt),2) & _
Right("0" & Second(tt),2))
End Function
感謝您的答案,我將如何迭代子文件夾中的文件?我認爲這可以解決我的問題。 – Cameron
嘗試運行您的新腳本,該腳本返回腳本名稱,以及我完成的下載目錄中最舊文件(在其子文件夾中)的日期,時間和名稱,這是向前邁進的一步,但仍不會刪除任何內容。還嘗試刪除前3個評論標記,這給了我在第20行的'權限被拒絕'。每個人都完全控制我的所有驅動器,並且我是管理員,所以我猜測錯誤來自空字符串再次。 – Cameron
我已經寫了關於我試圖達到什麼[這裏](http://stackoverflow.com/questions/36259830/automatically-delete-oldest-files-and-folders-from-a-directory-when-限制在gb),我會很感激你的幫助找到有效的東西。 – Cameron