go up one folder level with FileSearch function

M

muyBN

With the FileSearch function, how would I instruct the .lookin field to
generically search a folder up one level relative to the folder which is
presently the default or operating folder?
 
H

Helmut Weber

Hi Bryan,

have a look at this sample,
remembering good old DOS-times.

With Application.FileSearch
.NewSearch
.LookIn = "c:\test"
MsgBox .LookIn
.LookIn = ".." ' <<<
MsgBox .LookIn
End With
 
M

muyBN

Groovy, good old DOS. The irony is that many DB programs have gone back to
using command prompts. I never used the command prompt in SQL Server
7--pre.NET. I recall using one dot, .\ but never had the opportunity of using
double-dot. Thanks for letting me know; it worked perfectly.
 
M

muyBN

I should say that when I initially put your code into my function it worked
fine, but as I got into my function deeper, I could never get the search
folder to change.

First, here's a little explanation of what I'm trying to do, then the code
that doesn't accomplish it all:

(1) From a sub, I pass file name criteria (strFile = "201_P05*.*", for
example) and the path in which a certain instance of it is found.
(2) Search for instances of the file criteria within the folder.
(3) If more than one instance of the file criteria is found in a folder,
then the files in this folder are the ones I want to work with and blnStop is
set to true and the looping stops.
(4) On the other hand, if only one file in the searched folder fits the
search criteria, move up one folder level (attempted to do this per your
demonstration of ".." but it didn't move to another folder).

This is my code:

Function umRefineIt(strFile As String, strPath As String) As String
Dim strData As String, dirArray() As String, strFiles As String
Dim intCnt As Integer, intFiles As Integer
Dim blnStop As Boolean
'declare initial size of array variable (shrink later)
ReDim dirArray(1000)
With Application.FileSearch
.NewSearch
.LookIn = strPath
.SearchSubFolders = True
.FileName = strFile
.Execute
intFiles = .FoundFiles.Count
While .Execute(SortBy:=msoSortByFileName,
SortOrder:=msoSortOrderAscending) > 0 And blnStop = False
If intFiles > 1 Then
blnStop = True
For intCnt = 1 To intFiles
If InStr(.FoundFiles(intFiles), "grading") = 0 Then
intFiles = intFiles + 1
Next intCnt
End If
intCnt = 0
.NewSearch
.LookIn = strPath
.LookIn = ".."
.SearchSubFolders = True
.FileName = strFile
.Execute
Wend
End With
 
M

muyBN

Beautiful! I hereby answer my own question that I happened to see in Word VBA
help:

ChDir ".." takes you up one directory from where you are.
 

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