AW: dummy Datei (txt) erstellen und löschen
20.10.2017 08:43:13
Peter(silie)
Hallo,
hier mal ein wenig Code zum rumprobieren:
(bla ist dein Sub mit der schleife,
Create_Textfile ist eine Function mit zwei Optionalen Parametern: P1 = Pfad, P2 = Inhalt,
Delete_File löscht die als Pfad angegebene Datei
Delete_all_Files_where_Name_like löscht alle dateien innerhalb eines Ordners deren name wie xy sind,
Defaultpath_VBAgent erstellt einen Dateinamen, dort musst du evtl. folgendes machen:
Format(Time, "hh-mm-ss") in Format(Time, "hh-mm-ss-ms"), denn wenn er in der schleife das aufruft um einen Namen zu generieren, und sich die Sekunde nicht verändert hat, überschreibt er die Datei)
Sub bla()
Dim tmpName As String
For i = 1 To lngZ
tmpName = "C:\TEMP\tmp_" & i & ".txt"
Create_Textfile tmpName
'hier löschen? Falls ja dann:
Delete_File tmpName
Next i
'oder hier löschen? Falls ja dann:
Delete_all_Files_where_Name_like "tmp_", "C:\TEMP\"
End Sub
Private Function Create_Textfile(Optional ByVal Save_Path As Variant, Optional ByVal Input_ As _
Variant) As Boolean
If IsMissing(Save_Path) Then Save_Path = Defaultpath_VBAgent & ".txt"
Dim file_ As Long
file_ = FreeFile
Open Save_Path For Output As #file_
If Not IsMissing(Input_) Then
Print #file_, Input_
End If
Close #file_
End Function
Private Function Delete_File(ByVal File_Path As String) As Boolean
On Error Resume Next
Kill File_Path
On Error GoTo 0
End Function
Private Function Delete_all_Files_where_Name_like(ByVal this_ As String, ByVal InFolder As _
String) As Boolean
Dim fso, ofolder, ofile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ofolder = fso.GetFolder(InFolder)
For Each ofile In ofolder.FileSystemObject
If InStr(1, ofile.Name, this_) > 0 Then
Kill ofile
End If
Next ofile
End Function
Private Function Defaultpath_VBAgent() As String
Defaultpath_VBAgent = Application.DefaultFilePath & "\DirectoryAgent_File_" & _
Format(Date, "dd-mm-yyyy") & "_" & Format(Time, "hh-mm-ss")
End Function