'如文件名为:abc.txt, 则返回"abc" Function GetFileTitle(sFileName) '求文件的名称(不含扩展名) Dim pos pos = InStrRev(sFileName, ".") '从字符串尾部向前搜索子串(".") if(pos = 0) Then '文件无扩展名 GetFileTitle = sFileName else GetFileTitle = Left(sFileName, pos - 1) end if End Function '删除sFolder目录下,nDay之前的日志(如nDay为3,则只保留今天、昨天、前天的日志) Function ClearLogs(sFolder, nDay) Dim sFileName, sExt, sTitle Dim fs, oFolder, oFiles set fs = CreateObject("Scripting.FileSystemObject") set oFolder = fs.GetFolder(sFolder) set oFiles = oFolder.Files for each file in oFiles sFileName = file.Name sExt = fs.GetExtensionName(file) '获取文件扩展名 if(LCase(sExt) = "log" And Len(sFileName) = 14) Then '文件名格式如:2011-02-14.log sTitle = GetFileTitle(sFileName) sNow = Date() '获取当前日期 sDiffDays = DateDiff("d", sTitle, sNow) '求时间差,以天为单位 if sDiffDays >= nDay Then file.Delete '删除日志文件 end if end if Next ClearLogs = True End Function Dim sFolder, sExt, message sFolder = "F:\Programming\Applications\VBScript\GG" Dim fs set fs = CreateObject("Scripting.FileSystemObject") ClearLogs sFolder, 6 '不接受函数返回值时,ClearLogs后不能加()