Jump to content

Lisp for opening a file in a specified folder


Recommended Posts

Posted

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

Posted

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)
  )

 

Posted
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.

 

 

Posted

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
Posted
(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
Posted

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
Posted

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
Posted

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

Posted

@ vudungcom

Please insert the line

(setvar "sdi" 0)

right before the

(command "fileopen.....

I will write longer after about 6 hrs. Cheers

 

 

Posted

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?

Posted (edited)
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
Posted

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)
)

 

Posted
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

Posted

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

Posted

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)))
      )    
    )
  )

 

Posted

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

Posted

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

Posted

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
Posted

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
Posted (edited)

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...