AW: PDF datein im Ordner u. Unterordner löschen
02.04.2019 10:57:19
Nepumuk
Hallo Stefan,
teste mal:
Option Explicit
Public Sub DeletePDF()
Const FOLDER_PATH As String = "D:Liste\KDW\Abteilung\Essen\Fisch\Brot\"
Dim astrFolders() As String, strReturn As String
Dim strFileName As String
Dim dtmDeleteBeforeDate As Date
Dim ialngFolders As Long
Do
strReturn = InputBox("Bitte Datum im Format TT.MM.JJJJ eingeben.", "Eingabe")
If StrPtr(strReturn) = 0 Then Exit Sub
If strReturn Like "##.##.####" Then
If IsDate(strReturn) Then
dtmDeleteBeforeDate = CDate(strReturn)
Exit Do
Else
Call MsgBox("Bitte ein gültiges Datum eingeben.", vbExclamation, "Hinweis")
End If
Else
Call MsgBox("Bitte das Format des Datums beachten.", vbExclamation, "Hinweis")
End If
Loop
astrFolders = GetFolders(FOLDER_PATH)
For ialngFolders = LBound(astrFolders) To UBound(astrFolders)
strFileName = Dir$(Pathname:=astrFolders(ialngFolders) & "*.pdf")
Do Until strFileName = vbNullString
If FileDateTime(Pathname:=astrFolders(ialngFolders) & strFileName) <= _
dtmDeleteBeforeDate Then Call Kill(Pathname:=astrFolders(ialngFolders) & strFileName)
strFileName = Dir$
Loop
Next
End Sub
Private Function GetFolders(ByVal pvstrPath As String) As String()
Dim astrFolders() As String
Dim strFolder As String, strPath As String
Dim ialngIndex1 As Long, ialngIndex2 As Long
Redim Preserve astrFolders(ialngIndex1)
astrFolders(ialngIndex1) = pvstrPath
ialngIndex1 = 1
ialngIndex2 = 1
strPath = pvstrPath
Do
strFolder = Dir$(Pathname:=strPath & "*", Attributes:=vbDirectory)
Do Until strFolder = vbNullString
If strFolder <> "." And strFolder <> ".." Then
If GetAttr(strPath & strFolder) And vbDirectory Then
Redim Preserve astrFolders(0 To ialngIndex1)
astrFolders(ialngIndex1) = strPath & strFolder & "\"
ialngIndex1 = ialngIndex1 + 1
End If
End If
strFolder = Dir$
Loop
If ialngIndex1 = ialngIndex2 Then Exit Do
strPath = astrFolders(ialngIndex2)
ialngIndex2 = ialngIndex2 + 1
Loop
GetFolders = astrFolders
End Function
Gruß
Nepumuk