List out just the text file name

Kinki

New Member
Joined
Jan 30, 2005
Messages
12
Hi,
I have some text files in the D:/temp/temp and I want to list them out in column B of a new spreadsheet. I know of .FoundFiles method which doesn't print out what i want.

Does anyone knows how to apply just the file eg. D:/temp/temp/testdata.txt into cell eg B6 onwards as testdata?


Regards.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hie
Try the following...

Code:

Sub Button1_Click()

With Application.FileSearch

.LookIn = "D:\temp\temp"

.FileType = msoFileTypeAllFiles
.SearchSubFolders = True
.Execute
End With


cnt = Application.FileSearch.FoundFiles.Count

For i = 1 To cnt
rng = "B" & i + 5
If Right(Application.FileSearch.FoundFiles.Item(i), 3) = "txt" Then
Range(rng).Value = Application.FileSearch.FoundFiles.Item(i)
End If

Next i

End Sub

Hope this would be helpful.

Regards
Asim
 
Upvote 0
Hi Asim,
It doesn't work well. I want only the value of testData only which is in D:\temp\temp\ drive and the file is testData.txt

Regards,
valerie
 
Upvote 0
Perhaps something like this ?

Code:
Sub TestListFiles()
Dim strFile As String
Dim x As Double

x = 6
strFile = Dir("D:\temp\temp \*.txt")

Columns("B:B").Clear
Do While Len(strFile) > 0
    Cells(x, 2) = strFile
    x = x + 1
    strFile = Dir()
Loop

'// Sort & Format cell range
With Range(Cells(6, 2), Cells(6, 2).End(xlDown))
    .Columns.AutoFit
    .Sort Key1:=Range("B6"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

End Sub
 
Upvote 0
Hi

Thank you thank you. Near to it :) but how do i remove the extension of the file in the value.
Value display should be testdata instead of testdata.txt.

Regards,
valerie
 
Upvote 0
thank you for the additional tips :wink: .

I have used delimiter instead

Dim file As Variant
file = Split(Trim(strFile), ".txt")
MsgBox file(0)
 
Upvote 0
How about something like this?
Code:
Dim I As Integer
Dim strFileName As String
    
    With Application.FileSearch
        .NewSearch
        
        .LookIn = "D:\temp\temp"
        .Filename = "*.txt"
        .SearchSubFolders = True
        .Execute
    
        For I = 1 To .FoundFiles.Count
        
            strFileName = Mid(.FoundFiles(I), InStrRev(.FoundFiles(I), "\") + 1)
            strFileName = Left(strFileName, Len(strFileName) - 4)
            Range("B" & I + 5) = strFileName
        Next I
    End With
 
Upvote 0

Forum statistics

Threads
1,203,455
Messages
6,055,540
Members
444,794
Latest member
HSAL

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