Results 1 to 5 of 5
  1. #1
    Todd84 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    5

    Mass Import Repetitive Txt Files

    My goal is to have a static directory address on my desktop be where I put thousands of txt files every week. Some nice features are as follows:

    1. all columns will have the same names from week to week
    2. the de-duping by hand has been flawless
    3. the files names are the same from week to week.

    My first step is to delete all files with the characters "_" and "-" present in the file names within this specific folder.

    Here is a bit of code I wrote that ends up crashing my computer. I've been learning VBA code for about 2 months so still some learning to do.

    Sub KillFile()
    Dim MyFile As String
    Dim File As String


    MyFile = Dir$("C:\Users\Desktop\Data\5 min\us\nyse\1\*.*")
    On Error Resume Next
    Do While MyFile <> ""
    If InStr(1, MyFile, "_", vbTextCompare) > 0 Then
    File = MyFile
    Kill (File)
    Else
    If InStr(1, MyFile, "-", vbTextCompare) > 0 Then
    File = MyFile
    Kill (File)
    End If
    Loop
    End Sub

    once this is done, there is a whole bunch of other steps I want to do. This first bit will help simplify later steps for me. Then I can just scroll right through the whole folder.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Doesn't really crash the computer - it appears to be stuck in an infinite loop. Unless you have a Break key on computer, just terminate Access with the X close or Windows Task Manager.

    Did you run Debug Compile? Missing an End If line or combine the Else and the following If into an ElseIf, however, your code still bombs. Consider:
    Code:
    Sub KillFile()
    Dim fs, fl, f, X
    Set fs = CreateObject("Scripting.FileSystemobject")
    Set fl = fs.GetFolder("C:\Users\Desktop\Data\5 min\us\nyse\1")
    Set f = fl.Files
    For Each X In f
        If InStr(1, X, "_", vbTextCompare) > 0 Or InStr(1, X, "-", vbTextCompare) > 0 Then
            If Dir(X) <> "" Then Kill (X)
        End If
    Next
    Also review http://www.allenbrowne.com/ser-59.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Todd84 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    5
    Gotcha. Not enough "end if "would create an infinite loop.

    Thank you for the info so much!! I hope for a few months learning, my code was a so-so beginner's attempt at it :-/

    In regard to the variable X in your code it is unassigned to a type. When you do the "for each x" this is OK for it to be unassigned? The "for each x" knows what to do so to speak?

    Lastly I notice you combine statements into one line more than I did. This does look nicer and is easier to read. Is there any processing benefit to this?

    Thanks again and also for the link!!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    About the infinite loop, on second thought, I don't know how that procedure could have even gotten into the loop at all. The procedure should have failed to compile and not even run.

    I edited my post and included the X in the Dim statement. None of the variables are explicitely declared so they will default as Variant type. Variables aren't required to be declared at all but it is good practice. Include Option Explicit line in every module header and declaration will be mandatory.

    No processing benefit to the one-liner.

    I added the If Dir() because in my test the code was 'seeing' a filename that wasn't really in the folder. I don't know where it was pulling it from but it certainly wasn't visible in the folder. I can only think something corrupted about my Windows file directory.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Todd84 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2014
    Posts
    5
    Worked like a charm! Thank you. This was fantastic and gave me some items to research for future use. I may need a newer laptop. My 2013 access froze for about 2 seconds with "not responding" and then was fine and task complete.

    Now for mass importing the thousand text files with uniform column names. i am worried this is going to crash my comp :-/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import all files
    By raytackettsells in forum Import/Export Data
    Replies: 4
    Last Post: 08-10-2012, 11:59 AM
  2. VBA to Mass Import from Excel
    By Kimbertha in forum Programming
    Replies: 1
    Last Post: 07-27-2012, 12:22 PM
  3. Import only Files not already imported
    By Rhino373 in forum Programming
    Replies: 1
    Last Post: 05-04-2011, 03:28 PM
  4. Repetitive Import Problem.
    By jasonbarnes in forum Import/Export Data
    Replies: 5
    Last Post: 02-18-2011, 11:09 AM
  5. How to import a mass set of xls files
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 11-24-2010, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums