Jump to content
MarcoW

How to copy a list of files from one directory to another?

Recommended Posts

MarcoW

Hello,

 

I have a need for following lisp: when executed it should check where the current CTB files are stored (with (getenv "PrinterStyleSheetDir"))).

 

It should copy all the *.ctb files into another folder that allready exists: "c:\program files\MarcoW\".

 

This is what I have found to be working for 1 file, in this case a text file:

 

 
(vl-file-copy (findfile "atextfile.txt") "c:\\atextfile.txt")

 

So I figured, I should make a list out of * .ctb files in the current PrinterStyleSheetDir. That is what I do not know how to do. All I find is getfiled functions. Because that prompts to select file, I cannot use it.

 

The "findfile" function uses the support file search paths so that won't work either.

 

Do you know a solution? Thanks in advance!

 

My attempt so far:

 

(defun c:CopyCtb ( / ); localizing later

 

(setq

 

; get current directory for *.ctb files

cPrinterStyleSheetDir (getenv "PrinterStyleSheetDir")

 

; get current directory program files

cProgramFiles (getenv "programfiles")

 

; the new *.ctb files directory

nPrinterStyleSheetDir (strcat cProgramFiles "\MarcoW\Plotter\")

 

); end of setq

 

; here the part to get all CTB files in the current "env PrinterStyleSheetDir"

 

(princ)); end of defun

Share this post


Link to post
Share on other sites
MarcoW

AlanJT's subroutine might be of help??... but I do not know how to use it...

 

 
;;; Copy entire contents of directory to new location (subfolders included)
;;; #Source - source folder to copy
;;; #Dest - destination directory (will be created if doesn't exist)
;;; Alan J. Thompson, 10.06.09
(defun AT:XCopyDirectory (#Source #Dest / *error* #Scr)
 (setq *error* (lambda (x) (and #Scr (vlax-release-object #Scr))))
 (cond ((findfile #Source)
        (setq #Scr (vlax-get-or-create-object "WScript.Shell"))
        (vlax-invoke-method #Scr "Run" (strcat "XCopy " #Source " /E /H /Q /Y /I " #Dest) 0)
       )
 ) ;_ cond
 (*error* nil)
) ;_ defun

Share this post


Link to post
Share on other sites
Lee Mac

Not as fancy as Alan's use of the Windows Script Host, but this is one way to copy all files in a directory (not including subdirectories).

 

(defun CopyFiles (dir typ dest)
 (vl-load-com)

 (foreach file (vl-directory-files dir typ 1)
   (vl-file-copy (strcat dir file) (strcat dest file)))

 (princ))

 

dir : Source Directory (use double backslashes)

typ : type of files (DOS pattern, eg "*.dwg")

dest : Destination Directory (must be valid)

Share this post


Link to post
Share on other sites
alanjt
AlanJT's subroutine might be of help??... but I do not know how to use it...

 

 
;;; Copy entire contents of directory to new location (subfolders included)
;;; #Source - source folder to copy
;;; #Dest - destination directory (will be created if doesn't exist)
;;; Alan J. Thompson, 10.06.09
(defun AT:XCopyDirectory (#Source #Dest / *error* #Scr)
 (setq *error* (lambda (x) (and #Scr (vlax-release-object #Scr))))
 (cond ((findfile #Source)
        (setq #Scr (vlax-get-or-create-object "WScript.Shell"))
        (vlax-invoke-method #Scr "Run" (strcat "XCopy " #Source " /E /H /Q /Y /I " #Dest) 0)
       )
 ) ;_ cond
 (*error* nil)
) ;_ defun

 

It will copy everything within one directory (including sub-directories) to another. If the destination does not exist, it will be created. I used XCopy to easily copy all sub-directories and WScript to avoid any annoying Dos windows.

Share this post


Link to post
Share on other sites
alanjt
Not as fancy as Alan's use of the Windows Script Host, but this is one way to copy all files in a directory (not including subdirectories).

 

(defun CopyFiles (dir typ dest)
 (vl-load-com)

 (foreach file (vl-directory-files dir typ 1)
   (vl-file-copy (strcat dir file) (strcat dest file)))

 (princ))

dir : Source Directory (use double backslashes)

typ : type of files (DOS pattern, eg "*.dwg")

dest : Destination Directory (must be valid)

 

I'm pretty sure vl-file-copy will fail if it can't find the destination directory.

Share this post


Link to post
Share on other sites
Lee Mac
I'm pretty sure vl-file-copy will fail if it can't find the destination directory.

 

Exactly, which is why I said the destination directory 'must be valid.'

Share this post


Link to post
Share on other sites
alanjt
Exactly, which is why I said the destination directory 'must be valid.'

Oops, missed that. I think I also have one similar to the one you posted in the subroutine thread I started.

Share this post


Link to post
Share on other sites
Lee Mac
Oops, missed that. I think I also have one similar to the one you posted in the subroutine thread I started.

 

No worries :)

Share this post


Link to post
Share on other sites
MarcoW
Not as fancy as Alan's use of the Windows Script Host, but this is one way to copy all files in a directory (not including subdirectories).

 

(defun CopyFiles (dir typ dest)
 (vl-load-com)

 (foreach file (vl-directory-files dir typ 1)
   (vl-file-copy (strcat dir file) (strcat dest file)))

 (princ))

 

dir : Source Directory (use double backslashes)

typ : type of files (DOS pattern, eg "*.dwg")

dest : Destination Directory (must be valid)

 

 

I am about to explode :x

 

Why is this not working?

 

 
(vl-load-com)

(setq

cPrinterStyleSheetDir (getenv "PrinterStyleSheetDir")
cProgramFiles (getenv "ProgramFiles")
nPrinterStyleSheetDir (strcat cProgramFiles "\\MarcoW\\Plotter\\")

(foreach file (vl-directory-files cPrinterStyleSheetDir "*.ctb" 1)
 (vl-file-copy
  (strcat cPrinterStyleSheetDir file) (strcat nPrinterStyleSheetDir file)))

 (princ)

Share this post


Link to post
Share on other sites
Lee Mac

Does this directory exist?

 

nPrinterStyleSheetDir

Share this post


Link to post
Share on other sites
MarcoW

yes it does but maybe i should make it a bit simple..?

like c:\\marcow\\

 

?

 

edit:

yes this works..

 

 
(vl-load-com)

(foreach file (vl-directory-files "c:\\map1\\" "*.txt" 1)
(vl-file-copy (strcat "c:\\map1\\" file) (strcat "c:\\map2\\" file)))
(princ)

I must have made a mistake...

Share this post


Link to post
Share on other sites
Lee Mac

I see your problem -

 

Bear in mind that

 

(getenv "PrinterStyleSheetDir")

 

Will not end with "\\", and when you are concatenating it with the file, it needs a "\\" to make it a valid filepath.

Share this post


Link to post
Share on other sites
MarcoW

You are right :-)

 

This is working now:

 

 

(vl-load-com)

 

(setq

cPrinterStyleSheetDir [b][color=red](strcat[/color][/b] (getenv "PrinterStyleSheetDir") [b][color=red]"\\")[/color][/b]

cProgramFiles (getenv "ProgramFiles")

 

nPrinterStyleSheetDir (strcat cProgramFiles "

Share this post


Link to post
Share on other sites
Lee Mac

You're welcome Marco :)

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

×