Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

copy file folder

11 REPLIES 11
Reply
Message 1 of 12
Anonymous
1646 Views, 11 Replies

copy file folder

does anyone know if there is a way to copy a file folder from within lisp or a script file? i know how to copy drawings and rename them and such but is there a way to copy a whole directory (containing maybe 3 or 4 drawings) and then rename the directory?
11 REPLIES 11
Message 2 of 12
Anonymous
in reply to: Anonymous

If you mean scripting at Windows level, I think yes, it is possible. You
might want to look into the Windows File System Object. The FileSystemObject
provides access to the computer file system and the ability to manage file,
folders and drives. Basic methods include the ability to create, delete,
move and copy files and directories. The core functions are all exposed
through the FileSystemObject, it can be accessed to using the CreateObject
method. This can be done with VisualLISP. If you get a grip of the
FileSystemObject and then you dump it, you will find some useful methods:

;;;==================================
(defun usefso ( / fsobj)
(vl-load-com)
(setq fsobj
(vlax-create-object "Scripting.FileSystemObject")
)
(vlax-dump-object fsobj T)
;;;++++++++++++++++++++++++++++
;;; You can do smething else here using fsobj
;;; like copying the folder and the files.
;;; Use CopyFolder and CopyFiles methods
;;;++++++++++++++++++++++++++++
(vlax-release-object fsobj)
(princ)
)
;;;==================================

Command: (usefso)
; IFileSystem3: FileSystemObject
; Property values:
; Drives (RO) = #
; Methods supported:
; BuildPath (2)
; CopyFile (3)
; CopyFolder (3)
; CreateFolder (1)
; CreateTextFile (3)
; DeleteFile (2)
; DeleteFolder (2)
; DriveExists (1)
; FileExists (1)
; FolderExists (1)
; GetAbsolutePathName (1)
; GetBaseName (1)
; GetDrive (1)
; GetDriveName (1)
; GetExtensionName (1)
; GetFile (1)
; GetFileName (1)
; GetFileVersion (1)
; GetFolder (1)
; GetParentFolderName (1)
; GetSpecialFolder (1)
; GetStandardStream (2)
; GetTempName ()
; MoveFile (2)
; MoveFolder (2)
; OpenTextFile (4)
T

HTH

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


a écrit dans le message de news: 6247514@discussion.autodesk.com...
does anyone know if there is a way to copy a file folder from within lisp or
a script file? i know how to copy drawings and rename them and such but is
there a way to copy a whole directory (containing maybe 3 or 4 drawings) and
then rename the directory?
Message 3 of 12
Kent1Cooper
in reply to: Anonymous

You can certainly do that with a script, though the way I know how to do it is not to copy the folder and then rename it, but rather to make the new folder first, then copy everything from the old folder into the new one:

shell
md X:\file\path\newfolder
shell
copy X:\file\path\oldfolder\*.* X:\file\path\newfolder

If there might be any sub-folders in there that you also want copied:

shell
md X:\file\path\newfolder
shell
xcopy X:\file\path\oldfolder X:\file\path\newfolder /s

--
Kent Cooper


pfeld wrote...
does anyone know if there is a way to...copy a whole directory...and then rename the directory?
Kent Cooper, AIA
Message 4 of 12
Anonymous
in reply to: Anonymous

okay i tried the shell command but it is creating the wrong directory this is what i am using
(command "shell" "md S:\\Projects\\29015 Kennedy Blvd\\APT Drawings\\newfolder")
it is creating a folder called "S:\\Projects\\29015" is there an issue with spaces?
Message 5 of 12
Anonymous
in reply to: Anonymous

Are you trying to rename the old directory or are you just copying contents from one folder to a new folder with a new name?
Message 6 of 12
Anonymous
in reply to: Anonymous

just want to copy a folder and rename it leaving the original folder untouched and the new folder the exact same but with a new name
Message 7 of 12
Anonymous
in reply to: Anonymous

figured it out thanks for all of ya'lls help that lead me to my answer
(command "shell" "xcopy \"S:\\Projects\\project title\\Level B\" \"S:\\Projects\\project title\\Level C\" /I")
Message 8 of 12
Anonymous
in reply to: Anonymous

ok good.

Here I made this for you.
{code}
( defun copy-or-move-files ( md_source md_destination move_type md_move )
( if ( vl-directory-files md_source move_type 1 )
( progn
( if ( not ( vl-directory-files md_destination )) ( vl-mkdir md_destination ) );i
( foreach md_n ( vl-directory-files md_source move_type 1 )
( vl-file-copy ( strcat md_source md_n ) ( strcat md_destination md_n ) )
( if md_move ( vl-file-delete ( strcat md_source md_n )) );i
);fe
( append ( list md_destination ) ( vl-directory-files md_destination move_type 1 ))
);p
( )
);i
);d
{code}


I know you want to just move drawings but you may want this for other things later. You can choose to move or to copy and also add a filter that will only select certain file types.


Example:
{code}
( copy-or-move-files "c:\\temp\\new folder\\" "c:\\temp\\new folder2\\" ".dwg" T )
( copy-or-move-files "c:\\temp\\new folder\\" "c:\\temp\\new folder2\\" ".dwg" nil )
{code}
the first will grab all drawings from "c:\\temp\\new folder\\" and put them in "c:\\temp\\new folder2\\"
and remove them from the old folder if the new directory does not exist then it will be created.

the second will copy them nil tells the program not to delete the original files
you can also use "*" for the file filter and it will grab all files.


Hope that does what you need 😄
Message 9 of 12
Kent1Cooper
in reply to: Anonymous

[It looks like you already found elsewhere that you need to put quotes around the filepath if you want spaces in it.]
--
Kent Cooper


pfeld wrote...
....i am using
(command "shell" "md S:\\Projects\\29015 Kennedy Blvd\\APT Drawings\\newfolder")
it is creating a folder called "S:\\Projects\\29015" is there an issue with spaces?
Kent Cooper, AIA
Message 10 of 12
Anonymous
in reply to: Anonymous

thank you very much for the code i am sure i will use it at some point
Message 11 of 12
Anonymous
in reply to: Anonymous

balisteor wrote:

> {code}
> ( defun copy-or-move-files ( md_source md_destination move_type md_move )
> ( if ( vl-directory-files md_source move_type 1 )
> ( progn
> ( if ( not ( vl-directory-files md_destination )) ( vl-mkdir md_destination ) );i
> ( foreach md_n ( vl-directory-files md_source move_type 1 )
> ( vl-file-copy ( strcat md_source md_n ) ( strcat md_destination md_n ) )
> ( if md_move ( vl-file-delete ( strcat md_source md_n )) );i
> );fe
> ( append ( list md_destination ) ( vl-directory-files md_destination move_type 1 ))
> );p
> ( )
> );i
> );d
> {code}

A few style points:

- It is a pretty much universal convention to write lisp code without
superfluous whitespace around a parenthesis: (defun ...)
instead of ( defun ... ). In your own code you can do whatever you like,
the compiler won't care, but for any human reading the program this is
painful. Also, means that if you get used to a non-standard style, it
makes reading books etc more difficult for you, as they all use a
different style than yours.

- Professionals read Lisp code based on the indentation, so unindented
code is almost unreadable. End comments are not worth the bother, it is
clearer to put all closing parens together and use the indentation to
show what got closed. Use comments for information not visible in the
source code: why something is done instead of how, what data structures
are used, where some other pertinent information can be found etc.

- This is a small program with no global variables, so no need for fancy
naming. "source" is easier to read than "md_source", and there is no
loss of information.

- Why the name "move_type" ? The documentation for vl-directory-files
uses "pattern", so why not use the same name, so there is no additional
cognitive load in mapping the different names to the same concept when
reading documentation.

- (append (list item) some-list)

produces the same result as

(cons item some-list)

so why not use the shorter version?


- There is a separate function for moving files (using renaming), so no
need to copy and delete.

- nil is easier to read than () when there are more parens near. Both
mean the same thing.


A little re-write along those lines:

{code}

(defun copy-or-move-files (source destination pattern move / files func)
(setq files (vl-directory-files source pattern 1))
(if files
(progn
(if (not (vl-file-directory-p destination))
(vl-mkdir destination))
(setq func (if move
vl-file-rename
vl-file-copy))
(foreach n files
(func (strcat source n)
(strcat destination n)))
(cons destination (vl-directory-files destination pattern 1)))
nil))


{code}


- Those local variables aren't necessary, just wanted to minimize writing.
- Got a little fancy here with the "func" variable. Too many tricks like
this may make the program difficult to read.


Just hoping the newsreader/web interface won't clobber the indentation...

--
Message 12 of 12
Anonymous
in reply to: Anonymous

Martti,

In my years of writing lisp programs I still have not quite settled on a specific style of writing.
I have seen many different ways of writing and I read them different than most people and have gotten used to the ( function thing, as well as naming variables with more information. ( when the program gets large I read it better )

Knowing that there are always different ways of writing one program. I don't bother myself with weather or not a professional will like it, but rather what makes sense to me and more importantly what works. as you mentioned the compiler does not care and therefore neither do I this is not an excuse .. but rather an idea of why I do things the way I do. Your input influences the way I do things and slowly I can be better.

again... I am always grateful to receive input from you.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta