2010-11-02 64 views
0

我想將特定目錄中的所有.dat文件(總是不同的名稱)連接到一個文本文件中。任何人都可以將我指向正確的方向嗎?在VBA中連接文件

回答

1
Sub a() 
    Dim spath As String 
    Dim intFileNum%, bytTemp As Byte 
    spath = "c:\kk\" 
    outFileNum = FreeFile 
    Open spath + "Outfile.out" For Binary Access Write As outFileNum 
    sdir = Dir$(spath & "*.dat", vbNormal) 
    Do Until LenB(sdir) = 0 
     inFileNum = FreeFile 
     Open spath + sdir For Binary Access Read Lock Read As inFileNum 
     Do While Not EOF(inFileNum) 
      Get inFileNum, , bytTemp 
      Put outFileNum, , bytTemp 
     Loop 
     Close inFileNum 
     sdir = Dir$ 
    Loop 
    Close outFileNum 
End Sub 

唯一的問題是,Dir$() has a weird iteration convention

HTH!

2

您可以VBA下運行DOS:

Set oWSH = CreateObject("WScript.Shell") 
    oWSH.Run ("%comspec% /c copy c:\docs\conc\*.dat c:\docs\conc\onefile.txt") 

您需要/ b開關,如果文件有一個終結,如果文件不返回結束,該行將會繼續:

abc 
defabc 
def 
0

我想上面的例子可能比我的版本低於 速度較快,但在2 MB音頻文件上面花了31秒比照19下方 和Len = 1或100或1000沒有什麼區別。也許DOS喘息更快。我的文件可以是128Kb波特率的小時。

n=0 
k=0 
Do Until LenB(sdir) = 0 
    inFileNum = FreeFile 
    Open spath + sdir For Binary Access Read Lock Read As inFileNum Len = 1000 
    Do While Not EOF(inFileNum) 
     Get inFileNum, k, bytTemp 
     k=k+1 
     Put outFileNum, n, bytTemp 
     n=n+1 
    Loop