Jump to content

create directory Visual Lisp


au-s

Recommended Posts

Hi guys.

I am a bit stuck here.

 

I began typing:

 
(if (not (vl-file-directory-p "U:\\Project1\\CAD_Xref_Files\\"))
(vl-mkdir "U:\\Project1\\CAD_Xref_Files\\"))

 

This works for one project called project1.

 

What if I want to use same lisp but in a Project2. Can I somehow make it choose the "Main_Path" which can be U:\\ProjectX\\ and then continue with CAD_Xref_Files or whatever I put in there.

Link to comment
Share on other sites

This is what I use to create a directory structure:

 

;;-------------------=={ Make Directory }==-------------------;;
;;                                                            ;;
;;  Creates a directory structure                             ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  dir - the directory to create                             ;;
;;------------------------------------------------------------;;
;;  Returns:  T if directory creation is successful, else nil ;;
;;------------------------------------------------------------;;

(defun LM:MakeDirectory ( dir / MakeDirectory folders )
 ;; © Lee Mac 2010
 ;; (LM:MakeDirectory "C:\\Folder\\Subfolder")
 (vl-load-com)

 (defun MakeDirectory ( root folders )
   (if folders
     (
       (lambda ( dir ) (vl-mkdir dir)
         (MakeDirectory dir (cdr folders))
       )
       (strcat root "\\" (car folders))
     )
   )
 )
 
 (if (setq folders (LM:str->lst (vl-string-translate "/" "\\" dir) "\\"))
   (MakeDirectory (car folders) (cdr folders))
 )
 (vl-file-directory-p dir)
)

;;-------------------=={ String -> List }==-------------------;;
;;                                                            ;;
;;  Separates a string into a list of strings using a         ;;
;;  specified delimiter string                                ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - string to process                                   ;;
;;  del - delimiter by which to separate the string           ;;
;;------------------------------------------------------------;;
;;  Returns:  A list of strings                               ;;
;;------------------------------------------------------------;;

(defun LM:str->lst ( str del / pos )
 ;; © Lee Mac 2010
 (if (setq pos (vl-string-search del str))
   (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
   (list str)
 )
)

Link to comment
Share on other sites

You have to validate/create the path one level at a time:

 

;;; STR2LST
;;; Transforms a string with separator into a list of strings
;;; Author: Gile
;;; Arguments
;;; str = the string
;;; sep = the separator pattern
(defun  str2lst(str sep / pos)
 (if (setq pos (vl-string-search sep str))
   (cons (substr str 1 pos)
     (str2lst (substr str (+ (strlen sep) pos 1)) sep)
   )
   (list str)
 )
) 

(setq MyPath "U:\\Project1\\CAD_Xref_Files"
     MyPath (str2lst MyPath "\\")
     thePath (car MyPath))
(foreach PathItem (cdr MyPath)
(if (not (vl-file-directory-p (setq thePath (strcat thePath "\\" PathItem))))
 (vl-mkdir thePath)
)
)

 

 

Regards,

Mircea

Link to comment
Share on other sites

Okay, but I might been unclear here. Sorry.

What I wanted is for the user to choose the main path. Then the lisp continues with making CAD_Xref or Path\\to\\CAD_xref\\

Link to comment
Share on other sites

What I wanted is for the user to choose the main path. Then the lisp continues with making CAD_Xref or Path\\to\\CAD_xref\\

 

What criteria is being used to determine the root path (if any)?

 

For example, if the needed criteria is related to the active drawing, one could use (or extract part of) the dwgprefix system variable.

Link to comment
Share on other sites

Yeah I tried with dwgprefix and then I made a relative path to the text file from the place of the dwg file.

I think that is better solution. It's locked to users in terms of that the folder-structure have to be always the same. In that case all is stored in an Admin fodler.

So, the problem is solved at least to my needs.

 

But the question was how to make the user choose a project path and then make the lisp to make other paths, like:

user chooses his project folder and lisp makes a structure of folders i.e,

So if user chooses folder the lisp makes additional folders so the full path will be P3/admin/cad or in the first case P1/admin/cad.

 

Allthough I found it in the end not very good in terms of cad manager issues. So, the getvar dwgprefix helped me a lot.

thanks

Link to comment
Share on other sites

Happy to help.

 

But the question was how to make the user choose a project path

 

 

... I beleieve, JohnM answered this already:

 

Look at the getfiled function. this will display a dialog box so the user can navigate to a path or file.
Link to comment
Share on other sites

But the question was how to make the user choose a project path

 

GetFileD(ialog) is good for a selection of file (as the name implies), I find this better for a selection of path:

 

;;-------------------=={ Directory Dialog }==-----------------;;
;;                                                            ;;
;;  Displays a dialog prompting the user to select a folder   ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  msg  - message to display at top of dialog                ;;
;;  dir  - root directory (or nil)                            ;;
;;  flag - bit coded flag specifying dialog display settings  ;;
;;------------------------------------------------------------;;
;;  Returns:  Selected folder filepath, else nil              ;;
;;------------------------------------------------------------;;

(defun LM:DirectoryDialog ( msg dir flag / Shell HWND Fold Self Path ac )
 ;; © Lee Mac 2010

 (setq Shell (vla-getInterfaceObject (setq ac (vlax-get-acad-object)) "Shell.Application")
       HWND  (vl-catch-all-apply 'vla-get-HWND (list ac))
       Fold  (vlax-invoke-method Shell 'BrowseForFolder (if (vl-catch-all-error-p HWND) 0 HWND)  msg flag dir))
 (vlax-release-object Shell)
 
 (if Fold
   (progn
     (setq Self (vlax-get-property Fold 'Self) Path (vlax-get-property Self 'Path))
     (vlax-release-object Self)
     (vlax-release-object Fold)      
     
     (and (= "\\" (substr Path (strlen Path)))
          (setq Path (substr Path 1 (1- (strlen Path)))))
   )
 )
 Path
)

 

Call using (or similar):

 

(LM:DirectoryDialog "Select Directory" nil 0)

 

Lee

Link to comment
Share on other sites

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