VBS 目次 - ray88’s diary
Function ConvertToDate(strDate)
On Error Resume Next
Dim year, month, day, hour, minute, second
year = Mid(strDate, 1, 4)
month = Mid(strDate, 5, 2)
day = Mid(strDate, 7, 2)
hour = Mid(strDate, 9, 2)
minute = Mid(strDate, 11, 2)
second = Mid(strDate, 13, 2)
ConvertToDate = DateSerial(year, month, day) & " " & TimeSerial(hour, minute, second)
If Err.Number <> 0 Then
ConvertToDate = "Invalid Date"
Err.Clear
End If
On Error GoTo 0
End Function
Function IsOlderThan(dateString, days)
Dim folderDate
folderDate = ConvertToDate(dateString)
If folderDate = "Invalid Date" Then
IsOlderThan = "Invalid Date"
Else
IsOlderThan = DateDiff("d", folderDate, Now) > days
End If
End Function
Sub DeleteSubFolder(folderPath)
Dim FileSystemObject
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
If FileSystemObject.FolderExists(folderPath) Then
FileSystemObject.DeleteFolder folderPath
End If
Set FileSystemObject = Nothing
End Sub
Dim ParentFolderPath, Days
ParentFolderPath = "ここに親フォルダパスを入力"
Days = 3
Dim FileSystemObject, ParentFolder, SubFolder, olderThanResult
Dim foldersToDelete
Set foldersToDelete = CreateObject("Scripting.Dictionary")
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
If FileSystemObject.FolderExists(ParentFolderPath) Then
Set ParentFolder = FileSystemObject.GetFolder(ParentFolderPath)
For Each SubFolder in ParentFolder.SubFolders
olderThanResult = IsOlderThan(SubFolder.Name, Days)
If olderThanResult = True Then
foldersToDelete.Add SubFolder.Path, SubFolder.Path
End If
Next
Dim folderPathToDelete
For Each folderPathToDelete in foldersToDelete.Keys()
FileSystemObject.DeleteFolder folderPathToDelete
Next
Else
End If
Set FileSystemObject = Nothing