Jump to content

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


MarcoW

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 "

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