Jump to content
vudungcom

Lisp for opening a file in a specified folder

Recommended Posts

vudungcom

Hi all,

 

I am looking for a lisp that can do some requirements

i have many drawings in one folder and sub folder for example:

in D:\Drawing, there are many drawings with the format: xA-xxxx, 1A-1234, 2A-4321, 3A-1234, sometime it has a tail 1A-1234yyy but yyy is not important, 1234 is drawing number, it is unique number, so what i want is:

1, I need a lisp that can open drawing 1A-1234 by inputting it into  a popup window of autocad and hit enter.

it means this lisp can search the drawing from specified folder and subfolder then open it

2, About inputting method, as i explained in above, instead of typing exactly 1A-1234, we can type xA-1234 or xA-1234yyy 

 

Thank you very much

Share this post


Link to post
Share on other sites
fuccaro

Maybe a simple solution will work best for you.

Load this super short Lisp and start it. In the “File Name” box enter any letter and press the “Find File” button. This will take you in the next dialog box. Enter the file name or just part of the file name if you prefer so. In the “Look In” put the starting folder and check the “Include subfolders”. Press the “Find now” button and wait some. AutoCAD will answer with a list of dwg files matching the entered name. Choose the desired one, press “OK”. You will be returned to the first dialog –just press the “Open” button.

Warning: the current drawing will close without saving!

(defun c:FindIt()
  (setq dwg (getfiled "Fuccaro" " " "dwg" 8))
  (command "fileopen" "y" dwg)
  )

 

Share this post


Link to post
Share on other sites
vudungcom
3 hours ago, fuccaro said:

Maybe a simple solution will work best for you.

Load this super short Lisp and start it. In the “File Name” box enter any letter and press the “Find File” button. This will take you in the next dialog box. Enter the file name or just part of the file name if you prefer so. In the “Look In” put the starting folder and check the “Include subfolders”. Press the “Find now” button and wait some. AutoCAD will answer with a list of dwg files matching the entered name. Choose the desired one, press “OK”. You will be returned to the first dialog –just press the “Open” button.

Warning: the current drawing will close without saving!


(defun c:FindIt()
  (setq dwg (getfiled "Fuccaro" " " "dwg" 8))
  (command "fileopen" "y" dwg)
  )

@fuccaro Thank you very much. it worked but in comparison with opening window explorer and search in it, it think it is same way, your method took many step.

 

 

Share this post


Link to post
Share on other sites
BIGAL

Fuccaro & vdungcom a couple of suggestions to narrow the search speed.

 

The (findfile) function will only search the current AutoCAD search path if a drive/directory prefix is not supplied.

 

To quote help getfile supports a starting directory

 

(getfiled "Title" "Directory Path and/or File name" "File Extension" Flag)
  • Like 1

Share this post


Link to post
Share on other sites
fuccaro
(defun c:FindIt()
  (setq path "C:\\Users\\miklos.fuccaro\\Documents")
  (setq DWGlist nil)
  (dwgs path)
  (setq fn (getstring "enter file name to search for "))
  (setq matches nil i 0)
  (foreach file DWGlist
    (cond
      ((wcmatch (vl-filename-base file) fn)(princ (strcat "\n" (itoa i) "   " file)))
      )
    (setq i (1+ i))
    )
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth (getint "\nenter number of file to open\n") DWGlist))
  )

(defun DWGs(path)	;grab all DWGs starting from PATH -including subfolders
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )

Something like this one?

Thanks BIGAL for your useful comment.

  • Like 1

Share this post


Link to post
Share on other sites
fuccaro

I've sent my previous post in a hurry, so let me add here a few sentences.

To search for a file named 1A-1234yyy, you should enter *1234*

All the files that match the name (and are in the search path, of course) are listed.

Enter the number of the desired file to start it.

 

The code could be improved a little, so that:

-if only one file is found, it is opened immediately

-if more files are found, they are all listed (as it does now) AND AutoCAD is switched to the text screen waiting for your input

Oh, and you must replace the search path in the second line of the program.

Does this help?

  • Like 1

Share this post


Link to post
Share on other sites
BIGAL

Fuccaro tried the code had a little problem with the foreach l1 lst but anyway got the dwglist to have a play. I used 2* as our dwgs all start with a year date eg 2018123.

 

Just a suggestion you can open the list up in say lee-mac List to dcl select the name rather than asking for an item number "ListBoxV1-2.lsp" There is though a maximum limit in the list box something like 256 items.

 


(if (not LM:listbox)(Load "listboxV1-2"))

(setq n (nth 0 (lm:listbox "Pick dwg" dwglist 2)))

(command "fileopen" "y" (nth n DWGlist))

  • Like 1

Share this post


Link to post
Share on other sites
vudungcom

Thank you @fuccaro, @BIGAL

i tried your lisp, after replacing the path, it says "Cannot run FILEOPEN if SDI mode cannot be established."

and i think it is better if we can select path from drop down list and we can change it whenever we want.

Thank you very much

Share this post


Link to post
Share on other sites
fuccaro

@ vudungcom

Please insert the line

(setvar "sdi" 0)

right before the

(command "fileopen.....

I will write longer after about 6 hrs. Cheers

 

 

Share this post


Link to post
Share on other sites
fuccaro

BIGAL, I tried the code on 4 computers, I had no problems at all with the foreach loop.

Vudungcom, did you try the solution I offered? If so, did it work for you?

Share this post


Link to post
Share on other sites
vudungcom
4 hours ago, fuccaro said:

BIGAL, I tried the code on 4 computers, I had no problems at all with the foreach loop.

Vudungcom, did you try the solution I offered? If so, did it work for you?

Sorry for late reply, i have tried but it seems can not search the drawing and open, i have already inserted the code (setvar "sdi" 0) as you said

Also there is no setting for selection drawing paths 

Edited by vudungcom

Share this post


Link to post
Share on other sites
CHLUCFENG

Maybe late to the game, but when I have multiple drawings open, and often from different directories, and I need to open a drawing in the same directory that my current drawing is in, I made this little quickie:

 

;Explore Current Directory
(defun c:xcd ()
  (command "shell"
     (strcat "explorer \"" (getvar "dwgprefix") "\"")
  )
  (princ)
)

 

Share this post


Link to post
Share on other sites
vudungcom
On 9/14/2018 at 10:24 PM, CHLUCFENG said:

Maybe late to the game, but when I have multiple drawings open, and often from different directories, and I need to open a drawing in the same directory that my current drawing is in, I made this little quickie:

 


;Explore Current Directory
(defun c:xcd ()
  (command "shell"
     (strcat "explorer \"" (getvar "dwgprefix") "\"")
  )
  (princ)
)

 

but it is not what i want

Share this post


Link to post
Share on other sites
BIGAL

fuccaro worked it out its to do with copy and pasting from the new website it is bringing unicode with it.

Share this post


Link to post
Share on other sites
fuccaro

Sorry to read about your copy/paste problems. Please find the attached text file -it is the same as the code posted here. Hopefully it will solve that unicode problem...  FindIt.lsp

Put any number of paths in the list and give it a try.

(defun c:FindIt()
  (setq path (list
	       "C:\\Users\\miklos.fuccaro\\Documents"
	       "C:\\"
	       "C:\\Users\\Public\\"
	       )
	i -1)
  (repeat (length path)
    (princ (strcat (itoa (setq i (1+ i))) "  " (nth i path) "\n"))
    )
  (textscr)
  (setq path (nth (getint "\n>>>>>> Enter path's No " ) path))
  (setq DWGlist nil)
  (dwgs path)
  (setq fn (getstring "enter file name to search for "))
  (setq matches nil i 0)
  (foreach file DWGlist
    (cond
      ((wcmatch (vl-filename-base file) fn)(princ (strcat "\n" (itoa i) "   " file)))
      )
    (setq i (1+ i))
    )
  (setq ToOpen (getint "\nenter number of file to open\n"))
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth ToOpen DWGlist))
  )

(defun DWGs(path)	;grab all DWGs starting from PATH -including subfolders
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )

 

Share this post


Link to post
Share on other sites
vudungcom

I still can not use your lisp, it always select wrong file name, exp: i type 2222 then it open file 1111,

mostly, it says invalid file name although these drawing exist

Share this post


Link to post
Share on other sites
fuccaro

My mistake. Please try this one:

(defun c:FindIt()
  (setq path (list
	       "C:\\Users\\miklos.fuccaro\\Documents"
	       "C:"
	       "C:\\Users\\Public"
	       )
	i -1)
  (repeat (length path)
    (princ (strcat "\n" (itoa (setq i (1+ i))) "  " (nth i path)))
    )
  (textscr)
  (setq path (nth (getint "\n>>>>>> Enter path's No " ) path))
  (setq DWGlist nil)
  (dwgs path)
  (setq fn (getstring "enter file name to search for "))
  (setq i -1)
  (repeat (length DWGlist)
    (setq file (nth (setq i (1+ i)) DWGlist))
    (cond
      ((wcmatch (vl-filename-base file) fn)(princ (strcat "\n" (itoa i) "   " file)))
      )
    )
  (setq ToOpen (getint "\nenter number of file to open\n"))
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth ToOpen DWGlist))
  )

(defun DWGs(path)	;grab all DWGs starting from PATH -including subfolders
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )

The path doesn't end with double back-slash!

(other mistakes are corrected too)

 

findit.LSP

Share this post


Link to post
Share on other sites
marko_ribar

I only weakly read this topic, but I had one sub function from my library that may be of interest...

 

(defun _findfiles ( libraryrootprefix filenamepattern / subs subfolderss subfs folders fl ) ;;; (_findfiles "F:\\ACAD ADDONS-NEW\\" "profile*.lsp")

  (defun subs ( folder )
    (setq subfolders (vl-remove "." (vl-remove ".." (vl-directory-files folder nil -1))))
    subfolders
  )

  (defun subfolderss ( rootfolder / subfolders )
    (subs rootfolder)
    (if subfolders
      (foreach sub subfolders
        (if (= (substr rootfolder (strlen rootfolder)) "\\")
          (progn
            (subfolderss (strcat rootfolder sub))
            (setq subfs (cons (strcat rootfolder sub) subfs))
          )
          (progn
            (subfolderss (strcat rootfolder "\\" sub))
            (setq subfs (cons (strcat rootfolder "\\" sub) subfs))
          )
        )
      )
    )
  )

  (if (/= (strlen libraryrootprefix) 3)
    (setq libraryrootprefix (vl-string-right-trim "\\" libraryrootprefix))
  )
  (subfolderss libraryrootprefix)
  (setq folders (append (list libraryrootprefix) subfs))
  (foreach folder folders
    (foreach x (vl-directory-files (strcat folder "\\") filenamepattern 1)
      (setq fl (cons (strcat folder "\\" x) fl))
    )
  )
  (reverse fl)
)

I hope you'll find it useful...

Regards, M.R.

  • Like 1

Share this post


Link to post
Share on other sites
BIGAL

My attempt, you need ListboxV1-2.lsp from Lee-mac.com also

 


(defun c:FindIt( / dwglist dwgnum pathnum path dwglist2)
  (setq path (list
           "C:\\Users\\miklos.fuccaro\\Documents"
           "C:\\Acadtemp"
           "C:\\Users\\Public"
           )
    i -1)
  ;(repeat (length path)
   ; (princ (strcat "\n" (itoa (setq i (1+ i))) "  " (nth i path)))
   ; )
  (textscr)
  (if (not LM:listbox)(load "listboxV1-2"))
  (setq pathnum (nth 0 (lm:listbox "Pick a path" path 2)))
  (setq path (nth pathnum path))
  ;(setq path (nth (getint "\n>>>>>> Enter path's No " ) path))
  (setq DWGlist nil)
  (dwgs path)
 ; (setq fn (getstring "enter file name to search for "))
(if (not AHgetval)(Load "getval"))
(AHgetval "Enter file name eg *222* " 25 24)
(setq fn item)
(setq dwglist2 '())
(setq i -1)
(repeat (length DWGlist)
(setq file (nth (setq i (1+ i)) DWGlist))
(cond
((wcmatch (vl-filename-base file) fn)(setq dwglist2 (cons file dwglist2)))
)
)
  ;(setq ToOpen (getint "\nenter number of file to open\n"))
  (setq dwgnum (nth 0 (lm:listbox "Pick dwg to open" dwglist2 2)))
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth dwgnum dwglist))
  )
;grab all DWGs starting from PATH -including subfolders
(defun DWGs(path)
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )

 

 

getval.lsp

  • Like 1

Share this post


Link to post
Share on other sites
vudungcom

Untitled.png.e668d0338ce79dc15ae0fc604362af32.png

3 hours ago, BIGAL said:

My attempt, you need ListboxV1-2.lsp from Lee-mac.com also

 

 


(defun c:FindIt( / dwglist dwgnum pathnum path dwglist2)
  (setq path (list
           "C:\\Users\\miklos.fuccaro\\Documents"
           "C:\\Acadtemp"
           "C:\\Users\\Public"
           )
    i -1)
  ;(repeat (length path)
   ; (princ (strcat "\n" (itoa (setq i (1+ i))) "  " (nth i path)))
   ; )
  (textscr)
  (if (not LM:listbox)(load "listboxV1-2"))
  (setq pathnum (nth 0 (lm:listbox "Pick a path" path 2)))
  (setq path (nth pathnum path))
  ;(setq path (nth (getint "\n>>>>>> Enter path's No " ) path))
  (setq DWGlist nil)
  (dwgs path)
 ; (setq fn (getstring "enter file name to search for "))
(if (not AHgetval)(Load "getval"))
(AHgetval "Enter file name eg *222* " 25 24)
(setq fn item)
(setq dwglist2 '())
(setq i -1)
(repeat (length DWGlist)
(setq file (nth (setq i (1+ i)) DWGlist))
(cond
((wcmatch (vl-filename-base file) fn)(setq dwglist2 (cons file dwglist2)))
)
)
  ;(setq ToOpen (getint "\nenter number of file to open\n"))
  (setq dwgnum (nth 0 (lm:listbox "Pick dwg to open" dwglist2 2)))
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth dwgnum dwglist))
  )
;grab all DWGs starting from PATH -including subfolders
(defun DWGs(path)
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )


 

 

 

getval.lsp

 

Thanks @BIGAL, it works fine now, one thing i would like to improve it is: we rarely change folder paths, so can you make it is default selection, when we need change, just input a command and change the path. it is only advance function, feel free to do it, but now it is ok for me. Thank you very much.

4 hours ago, fuccaro said:

My mistake. Please try this one:


(defun c:FindIt()
  (setq path (list
	       "C:\\Users\\miklos.fuccaro\\Documents"
	       "C:"
	       "C:\\Users\\Public"
	       )
	i -1)
  (repeat (length path)
    (princ (strcat "\n" (itoa (setq i (1+ i))) "  " (nth i path)))
    )
  (textscr)
  (setq path (nth (getint "\n>>>>>> Enter path's No " ) path))
  (setq DWGlist nil)
  (dwgs path)
  (setq fn (getstring "enter file name to search for "))
  (setq i -1)
  (repeat (length DWGlist)
    (setq file (nth (setq i (1+ i)) DWGlist))
    (cond
      ((wcmatch (vl-filename-base file) fn)(princ (strcat "\n" (itoa i) "   " file)))
      )
    )
  (setq ToOpen (getint "\nenter number of file to open\n"))
  (princ "\n>>>>>>")
  (command "fileopen" "y" (nth ToOpen DWGlist))
  )

(defun DWGs(path)	;grab all DWGs starting from PATH -including subfolders
  (setq lst (vl-directory-files path))
  (foreach l1 lst    
    (cond
      ((or (= l1 ".")(= l1 "..")) nil)
      ((vl-file-directory-p (strcat path "\\" l1))(dwgs(strcat path "\\" l1)))
       ((= (vl-filename-extension l1) ".dwg") (setq DWGlist (cons (strcat path "\\" l1) DWGlist)))
      )    
    )
  )

The path doesn't end with double back-slash!

(other mistakes are corrected too)

 

findit.LSP

Thank for your effort but when i input "1" or "4", it also error. please check attachment for detail.

fLtdfUw.png

 

Untitled.png

Edited by vudungcom

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×