AW: Umbenennen von Textfile mit String aus Textfile
07.02.2008 10:29:10
Textfile
Hallo Alexandra,
das könnte z.B. so gehen:
Public Sub TextDatUmbenennen2()
Dim bolDate As Boolean, bolName As Boolean
Dim myFileSystemObject, myFiles
Dim strPath As String, strHelp As String, strDate As String, strNewName As String
Dim strName As String
Dim arrInput() As String
Dim lngPos As Long, lngHelp As Long
' Pfad natürlich anpassen
strPath = "C:\Copy\Test"
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
For Each myFiles In myFileSystemObject.GetFolder(strPath).Files
' Nur Textfiles
If myFiles.Name Like "*.txt" Then
' datei einlesen
Open myFiles For Binary As #1
strHelp = Space(LOF(1))
Get #1, , strHelp
arrInput = Split(strHelp, vbCrLf)
Close #1
' Nun die Zeile mit dem Datum und Namen suchen
lngHelp = 0
bolDate = False
bolName = False
For lngPos = LBound(arrInput) To UBound(arrInput)
If InStr(arrInput(lngPos), "DATE") > 0 Then
strDate = Trim(Replace(arrInput(lngPos), "DATE", ""))
bolDate = True
End If
If InStr(arrInput(lngPos), "NAME") > 0 Then
strName = Trim(Replace(arrInput(lngPos), "NAME", ""))
bolName = True
End If
If bolName And bolDate Then
' Nun Datei umbennen. Wenn Dateiname bereits vorhanden, Namen nicht ändern.
On Error Resume Next
Name myFiles As strPath & "\" & strDate & "_" & strName & ".txt"
On Error GoTo 0
Exit For
End If
Next lngPos
End If
Next myFiles
End Sub
Gruß Heiko
PS: Rückmeldung wäre nett !