Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1152to1156
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Mehrere .csv Dateien zusammenführen

Mehrere .csv Dateien zusammenführen
karlheinz
Hi Leute,
ich habe viele Verzeichnisse in denen mehrere .csv Dateien liegen. Diese sind immer gleich.
Die Daten aus einer dieser Dateien (products.csv) möchte ich in einer Datei zusammengefast haben ohne das ich jede öffnen und Kopieren muss.
Geht das?
Und wenn ja, wie?
Danke für Hilfe.
Gruß Kalle
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 12:55:43
Tino
Hallo,
teste mal diese Version.
Private Declare Function GetShortPathNameA Lib "kernel32" ( _
    ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, _
    ByVal cchBuffer As Long) As Long
Public Function ShortPath(ByRef Path As String) As String
  Dim n As Long
 
  ShortPath = Space$(256)
  n = GetShortPathNameA(Path, ShortPath, 255)
  ShortPath = Left$(ShortPath, n)
End Function

Sub Test()
Dim strPath$, DosString$
'hier Ordner angeben 
strPath = "D:\Dein Ordner\Dein Unterordner\"
'Pfad Dos Kompatibel machen 
strPath = ShortPath(strPath)

If strPath <> "" Then
    strPath = IIf(Right$(strPath, 1) = "\", strPath, strPath & "\")
    DosString = strPath & "*.csv, " & strPath & "AllFiles.csv"
    Shell ("cmd.exe /c copy " & DosString)
Else
    MsgBox "Ordner nicht gefunden!", vbCritical
End If
End Sub
Gruß Tino
Anzeige
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 13:04:06
karlheinz
Muss ich ein neues Excel-Sheet öffnen und das da rein schreiben?
Es soll immer nur eine Datei von mehreren aus dem Verzeichnis zusammengeführt werden. Diese heist immer gleich das Verzeichnis nicht. Muss ich den Namen der Datei nicht angeben?
gruß
kalle
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 13:19:52
Tino
Hallo,
es werden alle csv Dateien die in diesen Ordner liegen zu einer zusammengefasst (AllFiles.csv)
Diese liegt danach im gleichen Ordner.
Es soll immer nur eine Datei von mehreren aus dem Verzeichnis zusammengeführt werden
Vertsehe ich nicht, eine Datei zusammenfassen? Aus einer Datei mach eine neue?
Gruß Tino
Anzeige
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 13:28:42
karlheinz
Hi,
nein.
Also, ich habe z.B. ein Verzeichnis
123.csv (die Verzeichnisse enden halt mit .csv sind aber Verzeichnisse.
und enthällt:
abc.csv
bcd.csv
abcd.csv
products.csv
das nächste Verzeichnis heist z.B.
234.csv
und enthällt ebenfalls
abc.csv
bcd.csv
abcd.csv
products.csv
das nächste z.B.
123456.csv
und enthällt ebenfalls
abc.csv
bcd.csv
abcd.csv
products.csv
diese Verzeichnisse liegen alle in einem Verzeichnis.
Jetzt möchte ich alle products.csv zusammenführen.
gruß
kalle
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 14:05:05
Tino
Hallo,
versuche es mal so.
kommt als Code in Modul1
Option Explicit 
Public meAr() 
Public Sub start2() 
Dim strFolder As String, sString As String, FileFilter As String 
Dim nCount As Long, lngFilecount As Long 
Dim strZiel$ 
 
 
strZiel = "D:\AllData.csv" 'Ziel für die zusammenfassung 
FileFilter = "*products.csv" 'Filter für die Suche 
 
strFolder = Trim$(fncGetFolder(sPath:="C:\")) 
 
If strFolder <> "" Then 
    strFolder = IIf(Right$(strFolder, 1) = "\", strFolder, strFolder & "\") 
    FindFiles strFolder, FileFilter, lngFilecount, True 
End If 
 
If lngFilecount > 0 Then 
    For nCount = Lbound(meAr) To Ubound(meAr) 
        LeseFile sString, meAr(nCount) 
        If sString <> "" Then 
            SchreibeInFile sString, strZiel 
        End If 
    Next nCount 
End If 
 
Erase meAr 
End Sub 
 
Sub LeseFile(ByRef sInhalt$, ByVal sFileName$) 
Dim F As Integer 
If Dir$(sFileName, vbNormal) <> "" Then 
    F = FreeFile 
    Open sFileName For Binary As #F 
    sInhalt = Space$(LOF(F)) 
    Get #F, , sInhalt 
    Close 
 End If 
End Sub 
 
Sub SchreibeInFile(ByRef sLine$, sFileName$) 
Dim F As Integer 
   
  F = FreeFile 
  Open sFileName For Append As #F 
  Print #F, sLine 
  Close #F 
   
  sLine = "" 
End Sub 
kommt als Code in Modul2
Option Explicit 
Option Private Module 
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long 
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" ( _
    ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long 
Private Declare Function FindClose Lib "kernel32" ( _
    ByVal hFindFile As Long) As Long 
 
Private Enum FILE_ATTRIBUTE 
    FILE_ATTRIBUTE_READONLY = &H1 
    FILE_ATTRIBUTE_HIDDEN = &H2 
    FILE_ATTRIBUTE_SYSTEM = &H4 
    FILE_ATTRIBUTE_DIRECTORY = &H10 
    FILE_ATTRIBUTE_ARCHIVE = &H20 
    FILE_ATTRIBUTE_NORMAL = &H80 
    FILE_ATTRIBUTE_TEMPORARY = &H100 
End Enum 
 
Private Const INVALID_HANDLE_VALUE = -1& 
Private Const MAX_PATH = 260& 
 
Private Type FILETIME 
    dwLowDateTime As Long 
    dwHighDateTime As Long 
End Type 
 
Private Type WIN32_FIND_DATA 
    dwFileAttributes As Long 
    ftCreationTime As FILETIME 
    ftLastAccessTime As FILETIME 
    ftLastWriteTime As FILETIME 
    nFileSizeHigh As Long 
    nFileSizeLow As Long 
    dwReserved0 As Long 
    dwReserved1 As Long 
    cFileName As String * MAX_PATH 
    cAlternate As String * 14 
End Type 
 
Private Declare Function MoveWindow Lib "user32.dll" ( _
    ByVal hwnd As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal bRepaint As Long) As Long 
Private Declare Function GetSystemMetrics Lib "user32.dll" ( _
    ByVal nIndex As Long) As Long 
Private Declare Function GetWindowRect Lib "user32.dll" ( _
    ByVal hwnd As Long, _
    ByRef lpRect As RECT) As Long 
Private Declare Function SHBrowseForFolder Lib "shell32" ( _
    lpbi As InfoT) As Long 
Private Declare Function CoTaskMemFree Lib "ole32" ( _
    ByVal hMem As Long) As Long 
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" ( _
    ByVal lpStr1 As String, _
    ByVal lpStr2 As String) As Long 
Private Declare Function SHGetPathFromIDList Lib "shell32" ( _
    ByVal pList As Long, _
    ByVal lpBuffer As String) As Long 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassname As String, _
    ByVal lpWindowName As String) As Long 
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal Msg As Long, _
    wParam As Any, _
    lParam As Any) As Long 
 
Private Type InfoT 
    hwnd As Long 
    Root As Long 
    DisplayName As Long 
    Title As Long 
    Flags As Long 
    FName As Long 
    lParam As Long 
    Image As Long 
End Type 
 
Private Type RECT 
    Left As Long 
    Top As Long 
    Right As Long 
    Bottom As Long 
End Type 
 
Public Enum BIF_Flag 
    BIF_RETURNONLYFSDIRS = &H1 
    BIF_DONTGOBELOWDOMAIN = &H2 
    BIF_STATUSTEXT = &H4 
    BIF_RETURNFSANCESTORS = &H8 
    BIF_EDITBOX = &H10 
    BIF_VALIDATE = &H20 
    BIF_NEWDIALOGSTYLE = &H40 
    BIF_BROWSEINCLUDEURLS = &H80 
    BIF_BROWSEFORCOMPUTER = &H1000 
    BIF_BROWSEFORPRINTER = &H2000 
    BIF_BROWSEINCLUDEFILES = &H4000 
    BIF_SHAREABLE = &H8000 
End Enum 
 
Private Const SM_CXFULLSCREEN = &H10 
Private Const SM_CYFULLSCREEN = &H11 
 
Private Const BFFM_SETSELECTION = &H466 
Private Const BFFM_INITIALIZED = &H1 
 
Private s_BrowseInitDir As String 
 
Public Function fncGetFolder( _
        Optional ByVal sMsg As String = "Bitte wählen Sie ein Verzeichnis", _
        Optional ByVal lFlag As BIF_Flag = BIF_RETURNONLYFSDIRS, _
        Optional ByVal sPath As String = "C:\") As String 
    Dim xl As InfoT, IDList As Long, RVal As Long, FolderName As String 
    s_BrowseInitDir = sPath 
    With xl 
        .hwnd = FindWindow("XLMAIN", vbNullString) 
        .Root = 0 
        .Title = lstrcat(sMsg, "") 
        .Flags = lFlag 
        .FName = FuncCallback(AddressOf BrowseCallback) 
    End With 
    IDList = SHBrowseForFolder(xl) 
    If IDList <> 0 Then 
        FolderName = Space(256) 
        RVal = SHGetPathFromIDList(IDList, FolderName) 
        CoTaskMemFree (IDList) 
        FolderName = Trim$(FolderName) 
        FolderName = Left$(FolderName, Len(FolderName) - 1) 
    End If 
    fncGetFolder = FolderName 
End Function 
 
Private Function BrowseCallback( _
        ByVal hwnd As Long, _
        ByVal uMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long 
    If uMsg = BFFM_INITIALIZED Then 
        Call SendMessage(hwnd, BFFM_SETSELECTION, ByVal 1&, ByVal s_BrowseInitDir) 
        Call CenterDialog(hwnd) 
    End If 
    BrowseCallback = 0 
End Function 
 
Private Function FuncCallback(ByVal nParam As Long) As Long 
    FuncCallback = nParam 
End Function 
 
Private Sub CenterDialog(ByVal hwnd As Long) 
    Dim WinRect As RECT, ScrWidth As Integer, ScrHeight As Integer 
    Dim DlgWidth As Integer, DlgHeight As Integer 
    GetWindowRect hwnd, WinRect 
    DlgWidth = WinRect.Right - WinRect.Left 
    DlgHeight = WinRect.Bottom - WinRect.Top 
    ScrWidth = GetSystemMetrics(SM_CXFULLSCREEN) 
    ScrHeight = GetSystemMetrics(SM_CYFULLSCREEN) 
    MoveWindow hwnd, (ScrWidth - DlgWidth) / 2, _
        (ScrHeight - DlgHeight) / 2, DlgWidth, DlgHeight, 1 
End Sub 
 
Sub FindFiles(ByVal strFolderPath As String, ByVal strSearch As String, _
        ByRef lngFilecount As Long, Optional SubFolder As Boolean = True) 
    Dim WFD As WIN32_FIND_DATA, lngSearch As Long, strDirName As String 
     
    lngSearch = FindFirstFile(strFolderPath & "*.*", WFD) 
     
    If lngSearch <> INVALID_HANDLE_VALUE Then 
        GetFilesInFolder strFolderPath, strSearch, lngFilecount 
        Do 
            If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then 
                strDirName = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1) 
                If SubFolder = False Then Exit Sub 'ohne Unterordner 
                If (strDirName <> ".") And (strDirName <> "..") Then _
                    FindFiles strFolderPath & strDirName & "\", strSearch, lngFilecount 
            End If 
        Loop While FindNextFile(lngSearch, WFD) 
        FindClose lngSearch 
    End If 
End Sub 
 
Sub GetFilesInFolder(ByVal strFolderPath As String, ByVal strSearch As String, _
        ByRef lngFilecount As Long) 
    Dim WFD As WIN32_FIND_DATA, lngSearch As Long, strFileName As String 
     
    lngSearch = FindFirstFile(strFolderPath & strSearch, WFD) 
    If lngSearch <> INVALID_HANDLE_VALUE Then 
        Do 
            If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> _
                FILE_ATTRIBUTE_DIRECTORY Then 
                strFileName = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1) 
                Redim Preserve meAr(lngFilecount) 
                meAr(lngFilecount) = strFolderPath & strFileName 'auflisten in Zelle 
                lngFilecount = lngFilecount + 1 
            End If 
        Loop While FindNextFile(lngSearch, WFD) 
        FindClose lngSearch 
    End If 
End Sub 
 
 
Gruß Tino
Anzeige
AW: Mehrere .csv Dateien zusammenführen
02.05.2010 14:23:10
karlheinz
Hi,
ich bekomme einen internen Fehler beim Kompilieren:
Private Type WIN32_FIND_DATA
gruß
kalle
AW: hier meine Testmappe...
02.05.2010 15:12:51
karlheinz
Hi noch mal,
als Win7 hab ich nur eine 64Bit Version.
Nun soll ich was konvertieren, aber was und wie?
gruß
kalle
oder versuch mal diese Version...
02.05.2010 17:09:19
Tino
Hallo,
Du kannst auch mal diese testen, kann aber bei vielen Ordnern etwas länger dauern.
https://www.herber.de/bbs/user/69361.xls
Gruß Tino
AW: oder versuch mal diese Version...
02.05.2010 18:38:38
karlheinz
Hi,
danke das funtioniert super. sogar unter XP.
Gibt es auch noch die Möglichkeit die erste Zeile (Kopfzeile) weg zu lassen und vieleicht die Leerzeile die zwischen zwei Dateien entsteht?
gruß
kalle

303 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige