Get textfile Name

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I have a text file that is always in a 16 digit alpha-numeric format ####-####-####-####.
How can I get this file name and insert it in a userform label?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Option Explicit

Private Sub UserForm_Initialize()
Me.Label1.Caption = ThisWorkbook.Name
End Sub
 
Upvote 0
The textfile is created using VBA (alpha-numeric format), which is place in folder name Beta. I need to get the name of this textfile name. Not the workbook that is currently open.
 
Upvote 0
.
The text file MUST be the ONLY FILE in the folder for the macro to work properly.


VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
 
Set oFolder = oFSO.GetFolder("C:\Users\gagli\Desktop\Beta") 'edit path as required
 
For Each oFile In oFolder.Files
 
    Me.Label1.Caption = oFile.Name
 
    i = i + 1
 
Next oFile

End Sub
 
Upvote 0
Unfortunately, the Beta folder will have 2 files. Any way around this?
 
Upvote 0
I tried this but it doesn't work. What I'm i missing...

VBA Code:
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Beta")
 
For Each oFile In oFolder.Files
 
    If oFile Like "####-####-####-####" Then Label1 = oFile.Name
 
Next oFile
 
Upvote 0
You haven't used the posted macro code.

What prevents the BETA folder from containing only one file ?
 
Upvote 0
.
This macro will search the BETA folder for a TEXT FILE. So long as your text file is the only text file in the folder ... this macro will work for you.

VBA Code:
Private Sub UserForm_Initialize()
Dim fc  As Integer
Dim fldr As FileDialog
Dim getfolder As String
Dim wrd As Variant, strfile As Variant
   
    getfolder = "C:\Beta"
    Set fldr = Nothing
     
    strfile = Dir(getfolder & "\*" & ".txt" & "*")
    Me.Label1.Caption = strfile

End Sub

The first macro posted requires only one file exist in the BETA folder. The second macro requires the folder contain only one text file.
You now have two different methods.
 
Upvote 0
Maybe I may have been misunderstood. There will be only 2 files (both are text files). One will be name in the aforementioned format (####-####-####-####.txt) and the other will be a regular name (i.e. Myfile.txt) The first code does not display the file name I wanted (as you said it requires the folder to have 1 file). Hence the reason I made an attempt with my code (which doesn't work).
BTW i did try the first code
 
Upvote 0
Tell me more about the other file in the folder.

Is it's name constant ?

What is the name ?

I understand it will always be there .... but what is it's purpose ?

Tell me why it needs to be there.

Please provide answers to each of the above. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

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