Importing folder of txt files

l KAUTION l

Board Regular
Joined
Nov 3, 2005
Messages
90
I have a folder of txt files that I would like to import. I would like each file to have its own table. I need to delimit the files using a specification named Delimiter. I was playing around with some code but have been unsuccessful so far. The code imports everything in the folder into one table and does not delimit the files. Any help would be much appreciated. Thanks in advance.

The code below is what I was playing with...

Private Sub Command0_Click()
Dim Counter As Integer

With Application.FileSearch
.NewSearch
.LookIn = "G:\PLSHARED\Conversion Team\~Temp" 'change this to your actual directory
.SearchSubFolders = False 'set to True if you want to search subfolders too
.FileName = "*.txt" 'get all files in the directory

If .Execute() > 0 Then 'files found
For Counter = 1 To .FoundFiles.Count 'loop through files
.FileName = .FoundFiles(Counter) 'set / get the file name
'Change the "ImportFile" part in the line below if you are using a different table name
DoCmd.TransferText acImportDelim, "Delimiter", "Import", .FileName, False 'import
DoEvents 'don't take over all of the PC resources
Next Counter
MsgBox "Import complete.", vbInformation, "Done"
Else 'files not found
MsgBox "There were no files found.", vbCritical, "Error"
End If
End With
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I got everything to import. I am still having problems with the files being delimited correctly and I am having problems with the names of the files. I would like the names of the files to include everything but ".txt". The following code is what I am currently using:

Private Sub Command0_Click()

Dim strfile As String
Dim theFilePath As String
Dim theFileName As String

ChDir ("G:\PLSHARED\Conversion Team\~Temp\")
theFilePath = "G:\PLSHARED\Conversion Team\~Temp\"
strfile = dir("*")
Do While Len(strfile) > 0
theFileName = Left(dir("*"), 1)
DoCmd.TransferText acImportDelim, "Delimiter", theFileName, theFilePath & strfile, True
Kill theFilePath & strfile 'Deletes the file
strfile = dir
Loop
End Sub
 
Upvote 0
Alright I found code to take off the left 4 characters, ".txt"

theFileName = Left(dir("*.txt"), Len(dir("*.txt")) - 4)

Now all I need to do is get the delimiter to work. Anyone have any suggestions?
 
Upvote 0
I fixed everything. For the future searchers here is my code. If there is better code out there that can do this job more efficiently please let me know.



Private Sub Command0_Click()

Dim strfile As String
Dim theFilePath As String
Dim theFileName As String

ChDir ("G:\PLSHARED\Conversion Team\~Temp\")
theFilePath = "G:\PLSHARED\Conversion Team\~Temp\"
strfile = dir("*.txt")
Do While Len(strfile) > 0
theFileName = Left(dir("*.txt"), Len(dir("*.txt")) - 4)
DoCmd.TransferText acImportFixed, "Delimiter", theFileName, theFilePath & strfile, True
Kill theFilePath & strfile 'Deletes the file
strfile = dir
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top