usenussi
Goto Top

Per VBA im Verzeichniss dateien mit Wildcard umbenennen

Ich habe mit Access 2010 eine Datenbank erstellt die aus Amazon Daten mit einigen Abfragen zur Übergabe eine Liste erstellt, die ich später in Excel in eine Steuertabelle exportiere.
Die Amazondaten werden im Downloadverzeichniss heruntergeladen als z.B.

C:\Users\Test\Downloads\ 2015Mär1-2015Mär31_CustomTransaction.csv und
C:\Users\Test\Downloads\report.txt

da sich die Datei ...Transaction.csv ständig ändert, möchte ich per VBA in meiner Datenbank vor dem einlesen automatisch die Datei im Pfad ändern.
also:
C:\Users\Test\Downloads\ Transaction.txt


Ich bin mit DoCmd.Rename C:\user\Test\Downloads\*.csv nicht zurecht gekommen.

auch keine Funktion mit:

Public Sub umbenennen()

Name "C:\Users\Test\Downloads\*.csv " As "C:\User\Test\*.txt"


End Sub

Es wäre schön, wenn mir jemand helfen könnte.(vielleicht mit Wildcard umbenennen \Pfad\*.csv auf \Pfad\Transaction.txt)
Vielen Dank im voraus
Udo

Content-Key: 313095

Url: https://administrator.de/contentid/313095

Ausgedruckt am: 19.03.2024 um 06:03 Uhr

Mitglied: 129813
129813 19.08.2016 aktualisiert um 20:02:08 Uhr
Goto Top
Set fso = CreateObject("Scripting.FileSystemObject")  
For Each f In fso.GetFolder("C:\user\Test\Downloads").Files  
     If LCase(fso.GetExtensionName(f.Name)) = "csv" Then  
        if fso.FileExists("C:\user\Test\Downloads\Transaction.txt") then fso.DeleteFile "C:\user\Test\Downloads\Transaction.txt",True  
	f.Name = "Transaction.txt"  
	Exit For
    End If
Next
Regards
Mitglied: usenussi
usenussi 19.08.2016 um 20:21:03 Uhr
Goto Top
Hallo vielen Dank für die schnelle Antwort.
ich habe die Zeilen im Modul gepackt wie Public Sub umbenennen()

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("C:\test").Files

If LCase(fso.GetExtensionName(f.name)) = "csv" Then

f.name = "Transaction.txt"

Exit For


End If

Next


End Sub

Ich habe aber keine Transaction.txt im Verzeichniss bekommen.
Mitglied: 129813
Lösung 129813 19.08.2016 aktualisiert um 20:38:23 Uhr
Goto Top
It works, tested it. It's renaming the file. You omitted the line which deletes an existing transaction.txt.

You can also use fso.MoveFile function to rename it, but this is actually the same like above ...
Sub rename_file()
   Const FOLDER = "C:\Test"  
   Set fso = CreateObject("Scripting.FileSystemObject")  
   For Each f In fso.GetFolder("C:\test").Files  
	If LCase(fso.GetExtensionName(f.Name)) = "csv" Then  
                if fso.FileExists(FOLDER & "\Transaction.txt") then fso.DeleteFile FOLDER & "\Transaction.txt",True  
		fso.MoveFile f.Path, FOLDER & "\Transaction.txt"  
		Exit For
	End If
   Next
End Sub
Line 6 deletes an existing transaction.txt otherwise the script will fail renaming the csv file if there is already one! You omitted the line above...

If this doesn't work you are makring a mistake i cannot see, sorry.
Mitglied: usenussi
usenussi 21.08.2016 um 11:45:22 Uhr
Goto Top
Vielen Dank für die tolle Lösung. Es hat mir sehr geholfen.
Danke