FileSearch - help

Z

-Zoki-

Hello to all

I have one problem which is probably peace of cake for you.
I have code below which in "list1" display list of all files from
path "TextBox4" and which begin with "TextBox3".

Let say that TextBox4 value is c:\test\ and TextBox5 value is 123456789.

When program run, in "List1" I get displayed file paths, for instance :
c:\test\123456789_1.jpg
c:\test\123456789_2.jpg
c:\test\123456789_3.jpg
c:\test\123456789_4.jpg

Problem is that I don't want whole path, I want only file names.
123456789_1.jpg
123456789_2.jpg
123456789_3.jpg
123456789_4.jpg

I guess that problem is stupid but I can't find solution.

Thanks in advance !


Code
----------------------------------------------------------------------------
-------------
Private Sub CommandButton7_Click()

Dim Path As String
Dim fsoFileSearch As FileSearch
Dim varFile As Variant

list1.Clear
List2.Clear

Path = (Me.TextBox4.Text)


Set fsoFileSearch = Application.FileSearch
On Error Resume Next
With fsoFileSearch
.NewSearch
.LookIn = "Path"
.Filename = (Me.TextBox4.Text) & "*" & ".jpg"
.SearchSubFolders = False

If .Execute(msoSortByFileName, msoSortOrderAscending, True) > 0 Then
For Each varFile In .FoundFiles

Me.list1.AddItem varFile


Next varFile
End If
End With

End Sub
 
T

Tom Ogilvy

Private Sub CommandButton7_Click()

Dim Path As String
Dim fsoFileSearch As FileSearch
Dim varFile As Variant
Dim v as Variant

list1.Clear
List2.Clear

Path = (Me.TextBox4.Text)


Set fsoFileSearch = Application.FileSearch
On Error Resume Next
With fsoFileSearch
.NewSearch
.LookIn = "Path"
.Filename = (Me.TextBox4.Text) & "*" & ".jpg"
.SearchSubFolders = False

If .Execute(msoSortByFileName, msoSortOrderAscending, True) > 0 Then
For Each varFile In .FoundFiles
v = split(varfile,"\")
Me.list1.AddItem v(ubound(v))
Next varFile
End If
End With

End Sub
 
Z

-Zoki-

Tom Ogilvy said:
Private Sub CommandButton7_Click()

Dim Path As String
Dim fsoFileSearch As FileSearch
Dim varFile As Variant
Dim v as Variant

list1.Clear
List2.Clear

Path = (Me.TextBox4.Text)


Set fsoFileSearch = Application.FileSearch
On Error Resume Next
With fsoFileSearch
.NewSearch
.LookIn = "Path"
.Filename = (Me.TextBox4.Text) & "*" & ".jpg"
.SearchSubFolders = False

If .Execute(msoSortByFileName, msoSortOrderAscending, True) > 0 Then
For Each varFile In .FoundFiles
v = split(varfile,"\")
Me.list1.AddItem v(ubound(v))
Next varFile
End If
End With

End Sub


Thank you for very fast response, it is working fine.
Now I noticed another problem with sorting.
This problem was present also before.

In command below:
----------------------------------------------------------------------------
------------------
If .Execute(msoSortByFileName, msoSortOrderAscending, True) > 0 Then
----------------------------------------------------------------------------
-----------------------

I should receive file names sorted by name, but this is not working good.
For instance , this files below are sorted in this way:

123456789_1_13.jpg
123456789_1_3397.jpg
123456789_1_3463.jpg
123456789_1_827.jpg
123456789_10_1626.jpg
123456789_10_2550.jpg
123456789_2_2705.jpg
123456789_2_556.jpg

And it should be sorted in this way (this si how it looks in win explorer):
123456789_1_13.jpg
123456789_1_827.jpg
123456789_1_3397.jpg
123456789_1_3463.jpg
123456789_2_556.jpg
123456789_2_2705.jpg
123456789_10_1626.jpg
123456789_10_2550.jpg

It looks like that this subroutine don't understand numbers in file names,
numbers
are represent as text and sorting is performed according that.

In this example my files are named only with numbers, but there are
situations
when they have some letter characters for instance N123456789_24_15.jpg
I there a possibility to solve this problem?

Best Regards!!
 
T

Tim Williams

When sorting, the filenames are treated as text, so your sort order is what would be expected.
Only solution is to either pad your numbers with 0's (eg. use "_002_" and not "_2_") or to separate the nubers and text and sort by
the two fields (the numbers being sorted as numeric).

I suspect the view you have from Windows Explorer may be sorted by date and not by filename, so maybe you could consider applying
the same sort in Excel.
 
Z

-Zoki-

Tim Williams said:
When sorting, the filenames are treated as text, so your sort order is what would be expected.
Only solution is to either pad your numbers with 0's (eg. use "_002_" and
not "_2_") or to separate the nubers and text and sort by
the two fields (the numbers being sorted as numeric).

I suspect the view you have from Windows Explorer may be sorted by date
and not by filename, so maybe you could consider applying
the same sort in Excel.

--

Hi , in Explorer files are sorted by file name and it sorted OK.
But never mind, I have bigger problem now, this complete code do not run on
my
computer on Job and several others computers, on my home computer
run perfectly *?$#&
I don't know what is happening but it looks like that this FileSearch can't
find any file.
I seen on google that lot of peoples have trouble with this FileSearch
function.


Zok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top