尝试删除文件时出现运行时错误800A0005

时间:2016-03-26 15:50:51

标签: vbscript

我运行.vbs时收到以下错误:

  

脚本:C:\ Users \ Me \ Desktop \ delete.vbs
  行:21
  Char:1
  错误:无效的过程调用或参数
  代码:800A0005
  来源:Microsoft VBScript运行时错误

这是代码。

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\PLEX\Downloads\Completed\")

intFolderSize = Int(((objFolder.Size / 1024) / 1024) / 1024)

Do While intFolderSize >= 79
  strOldestFile = ""
  dtmOldestDate = Now

  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

  objFSO.DeleteFile(strOldestFile)

  intFolderSize = Int(((objFolder.Size / 1024) / 1024) / 1024)
Loop

错误在objFSO.DeleteFile(strOldestFile)行。

有人可以告诉我为什么,以及如何解决它?

1 个答案:

答案 0 :(得分:1)

strOldestFile似乎是在strOldestFile = ""语句中初始化的长度为零的字符串。

  

Size Property

     

对于文件,返回指定文件的大小(以字节为单位)。对于   文件夹,返回所有文件和子文件夹的大小(以字节为单位)   包含在文件夹中。

     

object.Size

     

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 WhileLoopDeleteFile);最旧的文件是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