Author Topic: Is it possible to search file name  (Read 4047 times)

0 Members and 1 Guest are viewing this topic.

Adesu

  • Guest
Is it possible to search file name
« on: February 22, 2007, 11:51:21 PM »
Hi Alls,
I'm still confuse how to searching a file name in one drive,it say "D:\",as the sample to clear this question is,if I or you forgot put location folder a file name,let say "testXX.dwg",you only remember 2 poin drive and file name.
How to find it
Code: [Select]
(defun c:test (/ )
  (setq opt (getstring t"\nEnter file name: "))
  (setq file_name (strcat opt ".dwg"))
  (setq drive "D:\"")
  ;(setq lst (vl-directory-files drive file_name 0))
  ;(setq test1 (member file_name lst))
  ........
  (princ)
  )

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Is it possible to search file name
« Reply #1 on: February 23, 2007, 12:24:18 AM »
Code: [Select]
;|
 Function of search of all folders inside of the root folders specified in the list.

 Arguments:
 P - the list of root folders.
 For example '("C:\\Program Files") or ' ("C:" "D:" "E:")

 To cause
 (GetFolders '("C:\\Program Files\\AutoCAD 2004\\Sample"))
 Returns the list of folders, including specified in the list of search.
 ' ("C:\\Program Files\\AutoCAD 2004\\Sample"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\ActiveX"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\Database Connectivity"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\DesignCenter"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VBA"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VisualLISP"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\ActiveX\\ExtAttr"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\ActiveX\\ExternalCall"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\Database Connectivity\\CAO"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VBA\\VBAIDEMenu"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VisualLISP\\activex"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VisualLISP\\External"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VisualLISP\\mdi-vlx"
    "C:\\Program Files\\AutoCAD 2004\\Sample\\VisualLISP\\reactors"
)
|;
(defun GetFolders (p)
 ;; By ElpanovEvgeniy
 ;; (GetFolders '("C:\\Program Files\\AutoCAD 2004\\Sample"))
 (if p
  (append
   p
   (GetFolders
    (apply (function append)
           (mapcar
            (function (lambda (b)
                       (mapcar (function (lambda (a) (strcat b "\\" a)))
                               (vl-remove ".."
                                          (vl-remove "." (vl-directory-files b nil-1))
                               )
                       )
                      )
            )
            p
           )
    )
   )
  )
 )
)

Code: [Select]
;|
 Function of search of a file or files in the specified folder and all enclosed folders
 The file is set by a name or a mask

 Arguments:
 P - an initial way of search,
 For example "C:" or "C:\\Program Files"
 F - the name of a file,
 Use WCMATCH symbols is possible.
 For example
 "*.dwg" - will find all dwg-files
 Or "acad*.lsp"

 To cause
 (getfile "acad*.lsp" "C:\\Program Files")
 Returns the list of files with full by up to them and the name without a mask.
 ' ("C:\\Program Files\\AutoCAD 2004\\Express\\acadinfo.lsp"
    "C:\\Program Files\\AutoCAD 2004\\Support\\acad2004.lsp"
    "C:\\Program Files\\AutoCAD 2004\\Support\\acad2004doc.lsp"
    "C:\\Program Files\\AutoCAD 2004\\Support\\acadinfo.lsp"
)
|;
(defun GetFile (f p)
 ;; By ElpanovEvgeniy
 ;; (getfile "acad*.lsp" "C:\\Program Files")
 (apply (function append)
        (cons (if (vl-directory-files p f)
               (mapcar (function (lambda (x) (strcat p "\\" x))) (vl-directory-files p f))
              ) ; _ if
              (mapcar (function (lambda (x) (GetFile f (strcat p "\\" x))))
                      (vl-remove ".." (vl-remove "." (vl-directory-files p nil-1)))
              ) ; _ mapcar
        ) ; _ cons
 ) ; _ apply
)

Code: [Select]
;|
 Function of search of a file inside of the root folders specified in the list
 And all enclosed folders
 Search stops on the first specified file
 The file is set by a name or a mask
 In comparison with function GetFile works more quickly,
 Since interrupts search, after a finding of a file.
 It is recommended for search of files with the unique name.

 Arguments:
 P - the list of root folders.
 For example '("C:\\Program Files") or '("C:" "D:" "E:")
 F - the name of a file,
 Use WCMATCH symbols is possible.
 For example:
 "a?ad.exe"

 To cause:
 (GetFirstFile "a?ad.exe" '("C:\\Program Files"))
 Returns a line - a full way to a file with the full name.
"C:\\Program Files\\AutoCAD 2004\\acad.exe"
|;
(defun GetFirstFile (f p)
 ;; By ElpanovEvgeniy
 ;; (GetFirstFile "a?ad.exe" '("C:\\Program Files"))
 (cond
  ((not p) nil)
  ((vl-directory-files (car p) f) (strcat (car p) "\\" (car (vl-directory-files (car p) f))))
  ((GetFirstFile
    f
    (append (mapcar (function (lambda (x) (strcat (car p) "\\" x)))
                    (vl-remove ".." (vl-remove "." (vl-directory-files (car p) nil-1)))
            ) ; _ mapcar
            (cdr p)
    ) ; _ append
   ) ; _ GetFirstFile
  )
 ) ; _ cond
) ; _ defun

« Last Edit: February 23, 2007, 08:42:38 AM by ElpanovEvgeniy »

Adesu

  • Guest
Re: Is it possible to search file name
« Reply #2 on: February 23, 2007, 01:03:18 AM »
Hi Elpanov,
Wow...that great,thanks.

Adesu

  • Guest
Re: Is it possible to search file name
« Reply #3 on: February 23, 2007, 03:15:06 AM »
Hi Elpanov,
Here my code after I completed,thanks for your share.
Code: [Select]
; sfnbd is stand for Search File Name Base on Drive
;        Design by  : Adesu <Ade Suharna>
;        Email      : mteybid@yuasabattery.co.id
;        Homepage   : http://www.yuasa-battery.co.id
;        Create     : 23 February 2007
;        Program no.: 0541/02/2007
;        Edit by    :

(defun GetFolders (p)         ; By ElpanovEvgeniy
  (if
    p
    (append
      p
      (GetFolders
(apply (function append)
       (mapcar
(function (lambda (b)
     (mapcar (function (lambda (a) (strcat b "\\" a)))
     (vl-remove ".."
       (vl-remove "." (vl-directory-files b nil-1))
       )
     )
     )
   )
p
)
       )
)
      )
    )
  )

(defun c:sfnbd (/ cnt drive fil file_name len leng lst name_file opt)
  (if
    (setq opt (getstring t"\nEnter file name: "))
    (progn
      (setq file_name (strcat opt ".dwg"))
      (setq len (strlen file_name))
      (setq drive (getstring "\nEnter drive name <D>: "))
      (if
(= drive "")
(setq drive "D:\\")
(setq drive (strcase (strcat drive ":\\")))
)
      (setq lst (GetFolders (list drive)))
      (setq lst (cdr lst))
      (setq leng (length lst))
      (setq cnt 0)
      (repeat
leng
(setq fil (nth cnt lst))
(setq name_file (vl-filename-base fil))
(setq name_file (strcat name_file ".dwg"))
(if
  (= file_name name_file)
  (alert (strcat "\nHere location your file name"
"\n"
fil))
  )             ; if
(setq cnt (1+ cnt))
)               ; reapet
      )                 ; progn
    )                   ; if
  (princ)
  )                     ; defun

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Is it possible to search file name
« Reply #4 on: February 23, 2007, 03:20:50 AM »
Hi Adesu,
Why, did not begin to use:
Code: [Select]
(getfile file_name drive)

Adesu

  • Guest
Re: Is it possible to search file name
« Reply #5 on: February 23, 2007, 03:29:01 AM »
Hi Elpanov,
I'm not yet focus to "(getfile file_name drive)"
I just test with "GetFolders",maybe next time I would try again,thanks.

Hi Adesu,
Why, did not begin to use:
Code: [Select]
(getfile file_name drive)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Is it possible to search file name
« Reply #6 on: February 23, 2007, 08:39:37 AM »
Very nice work Evgeniy. :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.