【问题标题】:How to convert/rename unknown file type in access vba for fixed imports specification如何在访问 vba 中为固定导入规范转换/重命名未知文件类型
【发布时间】:2019-01-31 08:04:16
【问题描述】:

我需要使用固定 (.txt) 导入规范将一堆文件导入访问。但是,虽然这些文件可以在记事本中很好地打开,但它们不是 .txt 文件,因此导入失败。

原始文件已压缩。我已经有下面的代码(提取的和不完整的)来解压缩它们并将它们放在一个文件夹中。每次导入后,解压后的文件都会被删除以释放空间。

  If LCase(Right(fil.Name, 3)) = "zip" Then
        Status "Unzip file '" & fil.Name & "'..."
        currentISO = Left(fil.Name, 2)
        UnZipFiles iZip, fil.Path
        iTimeEnlapsed = 0
        'Pause until the file is completely extracted
        Do Until fso.GetFolder(iZip).Files.Count > 0
                Call Sleep(Int(fil.Size / (2 * 20)))
            'If unzip failed after 15sec it will be invoked again
            iTimeEnlapsed = iTimeEnlapsed + 1000
            If iTimeEnlapsed > 50000 Then
                If MsgBox("UnZip Error: " & fil.Name & vbCrLf & vbCrLf & "Do you want to SKIP this file and resume IMPORT process?", vbYesNo + vbQuestion, "UNZIP Errot - Resume ") = vbYes Then
                     Exit Do
                End If

               UnZipFiles iZip, fil.Path
               iTimeEnlapsed = 0
               Exit Do
            End If
        Loop
        For Each fil2 In fso.GetFolder(iZip).Files
            'Import the files (after converting them to .txt)

        DoCmd.TransferText acImportFixed, "Daily_Import", "Import_Data_Dump", fil.Path

        Kill fil2   

        Next fil2
        Set fil2 = Nothing
    Else

  End If

在上面的代码之后(对于每个 fil2),导入是从文件路径(很容易更改)完成的。但是,如果我的文件不以 .txt 结尾,这将不起作用。它们都是 XXXXX 而不是 XXXXX.txt,它们没有文件扩展名

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    只需重命名文件:

       For Each fil2 In fso.GetFolder(iZip).Files
           fil2.Name = fil2.Name & ".txt"
    
           ' Import the file.
           DoCmd.TransferText acImportFixed, "Daily_Import", "Import_Data_Dump", fil2.Path
    
           fil2.Delete
       Next fil2
    

    【讨论】:

    • 该死的,我从没想过这会奏效。我现在感觉很笨......谢谢@Gustav
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 2020-03-23
    • 2022-06-17
    相关资源
    最近更新 更多