find folder without full name?

find folder without full name?

mid-awe
Collaborator Collaborator
1,396 Views
6 Replies
Message 1 of 7

find folder without full name?

mid-awe
Collaborator
Collaborator

Hi all,

 

I have searched all over the web but still I can find no clue how to do what I need.

 

I need to determine if a folder exists in a specified root directory and then get it's FULL name. That is precisely the challenge. I have a folder set up for each year on drive "G:", so it would look like "G:\2017\", then inside of the 2017 folder I have each client folder including the client's country, city, and state. Here is the part I don't know; after the name and location is sometimes an additional suffix that I cannot guess.

 

An example of a folder name is, "G:\2017\MOUNT CANADA, BURLINGTON, ONTARIO, DIST". The suffix in this case tells me to interact with the distributor instead of directly contacting the client. 

 

I need to find the fully qualified path from the portion of the folder name that I do know.

Example: I know this much, "G:\2017\MOUNT CANADA, BURLINGTON, ONTARIO"

But I need, "G:\2017\MOUNT CANADA, BURLINGTON, ONTARIO, DIST"  to be returned from a search function.

 

I have not found any way to do it. I tried searching for the directory with a wcmatch but my attempts all failed. 

 

Please help if you can.

 

All suggestion and/or advice is welcome. Thank you in advance.

 

0 Likes
Accepted solutions (2)
1,397 Views
6 Replies
Replies (6)
Message 2 of 7

phanaem
Collaborator
Collaborator

Try this

 

(defun find_dir (str / key dir r)
  (setq key (vl-filename-base str)
        dir (vl-string-subst "" key str)
        key (strcase (strcat "*" key "*"))
  )
  (foreach sub (cddr (vl-directory-files dir "*.*" -1))
    (if
      (wcmatch (strcase sub) key)
      (setq r (cons (strcat dir sub) r))
      )
    )
  (reverse r)
)

(find_dir "G:\\2017\\MOUNT CANADA, BURLINGTON, ONTARIO") should return a list of all folders starting with "MOUNT CANADA, BURLINGTON, ONTARIO"

 

0 Likes
Message 3 of 7

phanaem
Collaborator
Collaborator
Accepted solution

Or this one

 

(defun find_dir (str / key dir)
  (setq key (vl-filename-base str)
        dir (vl-string-subst "" key str)
  )
  (mapcar
    '(lambda (a)
       (strcat dir a)
     )
    (vl-directory-files dir (strcat key "*.*") -1)
  )
)
0 Likes
Message 4 of 7

Ranjit_Singh
Advisor
Advisor
Accepted solution

Hi. Try below code. Make sure to pass the folder name as G:\\2017\\MOUNT CAN... using double backslash and without enclosing string quotes. Another way would be to use (getfiled "" "" "" 0) and just browse to the folder.

(defun c:somefunc  (/ fname1)
  (setq fname1 (getstring "\nEnter folder path: " T))
  (if (= "" (vl-filename-base fname1))
    (print "Need to enter part of folder name")
    (mapcar '(lambda (x)
               (if (wcmatch x (strcat (vl-filename-base fname1) "*"))
                 (princ (strcat (vl-filename-directory fname1) "\\" x))))
            (vl-directory-files (vl-filename-directory fname1))))
  (princ))
0 Likes
Message 5 of 7

mid-awe
Collaborator
Collaborator

Nicely done Smiley Happy

 

The first version failed. I think it returned every folder in my 2017 directory. I tried the second version and like a charm it worked.

 

Thank you so much.

0 Likes
Message 6 of 7

mid-awe
Collaborator
Collaborator

Thank you,

 

I accepted as a solution because it does work even though I entered my folder name with the double backslashes and the returned format is a combination of double backslashes & single backslashes. Perhaps I misunderstood the instructions?

 

example:

"G:\\2017\\MOUNT CANADA, BURLINGTON, ONTARIO"

& it returned:

"G:\\2017\MOUNT CANADA, BURLINGTON, ONTARIO, DIST"

 

Even so, it is clearly fixable.

0 Likes
Message 7 of 7

Ranjit_Singh
Advisor
Advisor

works just fine at my end.

C:SOMEFUNC
Command: SOMEFUNC
Enter folder path: C:\\Users\\ranj\\Desktop\\New folder (3)\\New folder\\MOUNT
C:\\Users\\ranj\\Desktop\\New folder (3)\\New folder\MOUNT CANADA, BURLINGTON, ONTARIO, DIST

 Edit: OK. I see the single slash at the end. Updated code below

(defun c:somefunc  (/ fname1)
  (setq fname1 (getstring "\nEnter folder path: " T))
  (if (= "" (vl-filename-base fname1))
    (print "Need to enter part of folder name")
    (mapcar '(lambda (x)
               (if (wcmatch x (strcat (vl-filename-base fname1) "*"))
                 (princ (strcat (vl-filename-directory fname1) "\\\\" x))))
            (vl-directory-files (vl-filename-directory fname1))))
  (princ))
Command: SOMEFUNC
Enter folder path: C:\\Users\\ranj\\Desktop\\New folder (3)\\New folder\\MOUNT
C:\\Users\\ranj\\Desktop\\New folder (3)\\New folder\\MOUNT CANADA, BURLINGTON, ONTARIO, DIST
0 Likes