这里是在VBScript中的另类,,遍历该目录只有一次。
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
strScriptName = WScript.ScriptFullName 'get script name
Set objFolder = objFS.GetFolder(strFolder)
Set dict = CreateObject("Scripting.Dictionary")
current = Now
temp=0
count=0 'count files
For Each strFile In objFolder.Files
If strFile.Path <> strScriptName Then 'do if not script name
count=count+1
dict.Add strFile.Path, strFile.DateLastModified
'if there are more than 21 files
If count >= 21 Then
flag=1
End If
End If
Next
filedate = dict.Items
filename = dict.keys
If flag = 1 Then
For i = 0 To dict.Count -1 'iterate dictionary
diffdate = DateDiff("d",filedate(i),now)
If num_days_diff >= temp Then
oldestfile = filename(i)
oldestdate = filedate(i)
temp = num_days_diff
End If
Next
End If
Set dict=Nothing
WScript.Echo "Oldest file: " & oldestfile, oldestdate
objFS.DeleteFile(oldestfile)
另存为deletefile.vbs和命令行
c:\test> cscript /nologo deletefile.vbs c:\path\somewhere
我会尝试你这个调试自己,但怎么样我只是告诉你我会怎么做。首先,坚持一些回声陈述。在前两个'SET'语句之后添加一行'echo countfiles =%countfiles%'和'echo BACKUPDIR =%BACKUPDIR%'。然后在'IF'语句中添加一行,如'echo countfiles> 21>。简单的调试技术可能会让您自己发现故障。 – 2009-11-23 00:18:39
@PP:我thik这将是一个很好的答案或至少一个很好的指针:) – 2009-11-23 00:23:50