오래된 파일 삭제 VBS (VB Script) Etc.2009. 6. 29. 17:49
strFolder = "C:\TEMP"
strExtensionUp = "TXT"
strExtensionLo = "txt"
DateDiffer = -30
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
Dim strFileName(), strFileCre(), Differ(), DelList()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
dateTime.Value = objItem.LocalDateTime
strToday = dateTime.GetVarDate
Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} Where " _
& "ResultClass = CIM_DataFile")
ReDim Preserve strFileName(colFileList.count), strFileCre(colFileList.count), Differ(colFileList.count), DelList(colFileList.count)
n = 0
d = 0
For Each objFile In colFileList
If objFile.Extension = strExtensionUp or objFile.Extension = strExtensionLo Then
strFileName(n) = objFile.Name
dateTime.Value = objFile.CreationDate
strFileCre(n) = dateTime.GetVarDate
Differ(n) = DateDiff ("D", strToday, strFileCre(n))
If Differ(n) < DateDiffer Then
DelList(d) = strFileName(n)
d = d + 1
End If
n = n + 1
End If
Next
strMessage = DateDiffer & "일 보다 오래된 다음 백업 파일들을 삭제합니다." & vbCrLf & vbCrLf
For i = 0 to d
strMessage = strMessage & DelList(i) & vbCrLf
Next
If d = 0 Then WScript.Quit
set objWSHShell = Wscript.CreateObject("Wscript.Shell")
WriteEvent = objWSHShell.LogEvent(4, strMessage, strComputer)
For i = 0 to d-1
strFilePath = DelList(i)
reFile = Replace(strFilePath,"\","\\")
set objFile = GetObject("winmgmts:\\"& strComputer & "\root\cimv2:CIM_Datafile.Name='" & reFile & "'")
objFile.Delete
Next