Import a text file with a name that changes

jmersing

Well-known Member
Joined
Apr 14, 2004
Messages
887
I need to set up an import from a text file into an Access table. Usually I would just create the link or use a transfer text macro. But my problem is that the name of the file will change every day. It will always begin with the date, after that it the name is somewhat random.

Any ideas?

Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Yes - your exact approach will vary based on how reliable your file naming situation is. If it's *always* named like you mention, then you can open up the folders and extract items that match your criteria and import them. If it may or may not...or if it's the result of a human naming it...or if maybe you just want to do it this way, you can open up the windows API File Open Dialog window and pick which file you want manually.

After that, the rest is easily automated.

Take a look at:

http://www.mrexcel.com/board2/viewtopic.php?t=85521&highlight=recallfilelocation

Something else you can also try is this using the filesystemobjects.
You'll need to set a reference to the MS Scripting Runtime.

Code:
Function rAppObject()
Dim fsoSysObj As FileSystemObject
Dim fdrFolder As Folder
Dim fdrSubFolder As Folder
Dim filFile As File
Dim strPath As String

strPath = UCase("C:\path\to\a\folder\")
'strPath = path_to_file_in_double_quotes

Set fsoSysObj = New FileSystemObject
    'Set fs = CreateObject("Scripting.FileSystemObject")
    'Set f = fs.GetFile(filespec)
Set fdrFolder = fsoSysObj.GetFolder(strPath)

For Each filFile In fdrFolder.Files
    Debug.Print filFile.DateLastAccessed & " " & filFile.Name
Next

End Function

Copy this code segment into a module and then play around and substitute items for the above ".DateLastAccessed". You can look at the various properties. There's a lot of useful things in here. This was just a demo function wrote myself long time ago as "notes" on how to do this.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,841
Messages
6,162,314
Members
451,759
Latest member
damav78

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