我目前有这个VBScript,用于检查文件夹的大小,然后删除文件(最早的文件),直到达到阈值。
Option Explicit
'use next line for production
On Error Resume Next
'use next line for debugging
'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\Torrent\"
strOldestFile = ""
Set oFolder = objFSO.GetFolder(strFolder)
intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)
Do While intFolderSize >= 110
strOldestFile = ""
dtmOldestDate = Now
FindOldestFile oFolder
'WScript.Echo strOldestFile
objFSO.DeleteFile strOldestFile, True
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
目前此脚本将:
我想修改脚本来执行此操作:
例如,是否可以让此脚本检查整个C:驱动器的已用容量,并在C上剩余少于5%时从C:\Users\PLEX\Downloads\Torrent\
删除最旧的文件:?
修改:我已获得完全访问权限,并通过将C:\
更改为C:
来修正错误。我还将C:\Users\PLEX\Desktop\New Folder
更改为\Users\PLEX\Desktop\New Folder
,现在没有错误。唯一的问题是IntFolderSize
仍然显示我的子文件夹的大小,而不是根。我已使用Wscript.Echo intfoldersize
确认了这一点。我目前的脚本如下。如何让IntFolderSize
显示根的使用大小?
option explicit
'use next line for production
'On Error Resume Next
'use next line for debugging
'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:"
strOldestFile = ""
Set oFolder = objFSO.GetFolder( strFolder)
intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)
Wscript.Echo intfoldersize
Do While intFolderSize >= 70
strOldestFile = ""
dtmOldestDate = Now
FindOldestFile objFSO.GetFolder(objFSO.BuildPath(strFolder,"\Users\PLEX\Desktop\New Folder\"))
'wscript.echo strOldestFile
objFSO.DeleteFile strOldestFile, True
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
答案 0 :(得分:0)
更改
FindOldestFile oFolder
到
FindOldestFile objFSO.GetFolder(objFSO.BuildPath(strFolder, "sub\folder"))
其中strFolder
是您的根文件夹,"sub\folder"
是该根文件夹下的子文件夹的相对路径。
修改:请注意oFolder.Size
属性会引发错误,除非您有权访问oFolder
下面的每个文件和文件夹。如果您想要检查卷上的可用磁盘空间,最好使用WMI并严格使用oFolder
来删除要删除的文件夹:
strFolder = "C:\Users\PLEX\Desktop\New folder"
Set oFolder = objFSO.GetFolder(strFolder)
threshold = ... 'desired minimum free diskspace in byte
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Volume WHERE DriveLetter='C:'"
Do While wmi.ExecQuery(qry).ItemIndex(0).FreeSpace < threshold
...
FindOldestFile oFolder
...
Loop