TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Marc'Antonio Alessi on September 16, 2022, 10:25:15 AM

Title: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 16, 2022, 10:25:15 AM
What do you think about this function to get a result similar to DOS_Copy?

Code - Auto/Visual Lisp: [Select]
  1. ; (ALE_Files_CopyDir "Z:\\Temp\\" "Z:\\Temp2\\" "Foo*.txt" 1)
  2. ; (ALE_Files_CopyDir "Z:\\Temp\\" "Z:\\Temp2\\" "Foo.txt" 1)
  3. ; SrcDir - [str] Source Directory
  4. ; DstDir - [str] Destination Directory
  5. ; FlsPat - [str] Pattern
  6. ; BitVal - [bol] 1= overwrite existing file
  7. ; BitVal - [bol] 2= move existing file
  8. ; Return Values  Type: List > if the copy was successful, otherwise nil.
  9. ;
  10. (defun ALE_Files_CopyDir (SrcDir DstDir FlsPat BitVal / FilLst FunNam OwrBit MovBit)
  11.   (or (wcmatch SrcDir "*[\\/]") (setq SrcDir (strcat SrcDir "\\")))
  12.   (or (wcmatch DstDir "*[\\/]") (setq DstDir (strcat DstDir "\\")))
  13.   (and
  14.     BitVal
  15.     (progn
  16.       (and (= 1 (logand 1 BitVal)) (setq OwrBit T))
  17.       (and (= 2 (logand 2 BitVal)) (setq MovBit T))
  18.     )
  19.   )
  20.   (if (setq FilLst (vl-directory-files SrcDir FlsPat 1))
  21.     (progn
  22.       (or (vl-file-directory-p DstDir) (vl-mkdir DstDir))
  23.       (setq FunNam (if MovBit vl-file-rename vl-file-copy))
  24.       (foreach ForElm FilLst
  25.         (if (findfile (setq DstFil (strcat DstDir ForElm)))
  26.           (and
  27.             OwrBit
  28.             (vl-file-delete DstFil)
  29.             (FunNam (strcat SrcDir ForElm) DstFil)
  30.           )
  31.           (FunNam (strcat SrcDir ForElm) DstFil)
  32.         )
  33.       )
  34.       (cons DstDir (vl-directory-files DstDir FlsPat 1))
  35.     )
  36.   )
  37. )
  38.  
Title: Re: Function similar to DOS_Copy
Post by: VovKa on September 16, 2022, 11:40:00 AM
What do you think about this function to get a result similar to DOS_Copy?
it's not safe to delete a file prior to successful copying
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 16, 2022, 03:12:20 PM
What do you think about this function to get a result similar to DOS_Copy?
it's not safe to delete a file prior to successful copying
You're right, it's the destination file anyway ... maybe it's better to rename it with a temporary name and then delete it after copying or rename it again if the copy fails?
(this because the vl-file-copy function will not overwrite an existing file).
Title: Re: Function similar to DOS_Copy
Post by: VovKa on September 16, 2022, 04:10:37 PM
maybe it's better to rename it with a temporary name and then delete it after copying or rename it again if the copy fails?
or copy to a temporary name and then if copying successful delete existing file and rename temporary
Title: Re: Function similar to DOS_Copy
Post by: BIGAL on September 16, 2022, 08:03:50 PM
You can use Shell and get at the old fashioned DOS commands XCOPY comes to mind.

(command "shell" ..... do your thing)

More recently starting to use Powershell inside windows. Can do stuff like unzip. Lots of help out there.
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 17, 2022, 03:28:01 AM
You can use Shell and get at the old fashioned DOS commands XCOPY comes to mind.

(command "shell" ..... do your thing)

More recently starting to use Powershell inside windows. Can do stuff like unzip. Lots of help out there.
Yes, with Shell you can do a lot of things but I think IMHO that in case of error you have less possibility of control.
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 17, 2022, 03:31:29 AM
Maybe this is more safe:
Code - Auto/Visual Lisp: [Select]
  1. ; (ALE_Files_CopyDir "Z:\\Temp\\" "Z:\\Temp2\\" "Foo*.txt" 1)
  2. ; Version 1.01
  3. ; SrcDir - [str] Source Directory
  4. ; DstDir - [str] Destination Directory
  5. ; FlsPat - [str] Pattern
  6. ; BitVal - [bol] 1= overwrite existing file
  7. ; BitVal - [bol] 2= move existing file
  8. ; Return Values  Type: List > if the copy was successful, otherwise nil.
  9. ;
  10. (defun ALE_Files_CopyDir (SrcDir DstDir FlsPat BitVal / FilLst FunNam OwrBit MovBit)
  11.   (or (wcmatch SrcDir "*[\\/]") (setq SrcDir (strcat SrcDir "\\")))
  12.   (or (wcmatch DstDir "*[\\/]") (setq DstDir (strcat DstDir "\\")))
  13.   (and
  14.     BitVal
  15.     (progn
  16.       (and (= 1 (logand 1 BitVal)) (setq OwrBit T))
  17.       (and (= 2 (logand 2 BitVal)) (setq MovBit T))
  18.     )
  19.   )
  20.   (if (setq FilLst (vl-directory-files SrcDir FlsPat 1))
  21.     (progn
  22.       (or (vl-file-directory-p DstDir) (vl-mkdir DstDir))
  23.       (setq FunNam (if MovBit vl-file-rename vl-file-copy))
  24.       (foreach ForElm FilLst
  25.         (if (findfile (setq DstFil (strcat DstDir ForElm)))
  26.           (and
  27.             OwrBit
  28.             (if (and (vl-file-rename DstFil (strcat DstFil ".tmp")) (FunNam (strcat SrcDir ForElm) DstFil))
  29.               (vl-file-delete (strcat DstFil ".tmp"))
  30.               (progn
  31.                 (vl-file-rename (strcat DstFil ".tmp") DstFil)
  32.                 (alert (strcat "File " SrcDir ForElm " not Copied or Renamed."))
  33.               )
  34.             )
  35.           )
  36.           (FunNam (strcat SrcDir ForElm) DstFil)
  37.         )
  38.       )
  39.       (cons DstDir (vl-directory-files DstDir FlsPat 1))
  40.     )
  41.   )
  42. )
Title: Re: Function similar to DOS_Copy
Post by: ronjonp on September 19, 2022, 03:35:26 PM
Have you seen this?
http://www.theswamp.org/index.php?topic=39415.msg446782#msg446782
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 20, 2022, 02:53:10 AM
Have you seen this?
http://www.theswamp.org/index.php?topic=39415.msg446782#msg446782
Thanks Ron, I will try your version and also LM:copyfolder (Lee Mac).
Grazie.  :-)
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 20, 2022, 09:40:06 AM
Xcopy is faster… tested with 430 existing files
Code: [Select]
(Benchmark '(
(ALE_Files_CopyDir "Z:\\Temp\\testfiles" "Z:\\Temp2\\testfiles" "*.*" 1)
(LM:copyfolder "z:\\temp\\testfiles" "z:\\temp2\\testfiles" T)
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
(ALE_Files_CopyDir "Z:\\Temp\\testfiles" "Z:\\Temp2\\testfiles" "*.*" 1)
(LM:copyfolder "z:\\temp\\testfiles" "z:\\temp2\\testfiles" T)
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
))
--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 16 iteration(s):
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......1171 / 30.61 <fastest>
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......1610 / 22.26
    (LM:copyfolder "z:\\temp\\tes..........).....16032 / 2.24
    (LM:copyfolder "z:\\temp\\tes..........).....16313 / 2.2
    (ALE_FILES_COPYDIR "Z:\\Temp\\testfi...).....27437 / 1.31
    (ALE_FILES_COPYDIR "Z:\\Temp\\testfi...).....35844 / 1 <slowest>
ALE_FILES_COPYDIR2 >> no vl-file-rename existing files but deleting before copy
Code: [Select]
(Benchmark '(
(ALE_Files_CopyDir2 "Z:\\Temp\\testfiles" "Z:\\Temp2\\testfiles" "*.*" 1)
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
(ALE_Files_CopyDir2 "Z:\\Temp\\testfiles" "Z:\\Temp2\\testfiles" "*.*" 1)
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
))
--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 16 iteration(s):
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......1375 / 17.62 <fastest>
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......2250 / 10.77
    (ALE_FILES_COPYDIR2 "Z:\\Temp\\testf...).....21671 / 1.12
    (ALE_FILES_COPYDIR2 "Z:\\Temp\\testf...).....24234 / 1 <slowest>
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 20, 2022, 09:53:21 AM
DOS_Copy
Code: [Select]
(Benchmark '(
(dos_copy "Z:\\Temp\\testfiles\\*.*" "Z:\\Temp2\\testfiles")
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
(dos_copy "Z:\\Temp\\testfiles\\*.*" "Z:\\Temp2\\testfiles")
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
))
--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 16 iteration(s):
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......2015 / 12.51 <fastest>
    (RJP-XCOPY "z:\\temp\\testfiles" "*....)......2063 / 12.22
    (DOS_COPY "Z:\\Temp\\testfiles\\*.*"...).....23531 / 1.07
    (DOS_COPY "Z:\\Temp\\testfiles\\*.*"...).....25203 / 1 <slowest>
Title: Re: Function similar to DOS_Copy
Post by: ronjonp on September 20, 2022, 01:11:26 PM
Might try the robocopy version too :)
http://www.theswamp.org/index.php?topic=39415.msg582702#msg582702
Title: Re: Function similar to DOS_Copy
Post by: Marc'Antonio Alessi on September 21, 2022, 08:29:40 AM
Might try the robocopy version too :)
http://www.theswamp.org/index.php?topic=39415.msg582702#msg582702
Yes... robocopy is faster…
Code: [Select]
(Benchmark '(
(rjp-robocopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/XD")
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
(rjp-robocopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/XD")
(rjp-xcopy "z:\\temp\\testfiles" "*.*" "z:\\temp2\\testfiles" "/d/c/i/k/r/y/q/s")
))

--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 64 iteration(s):
    (RJP-ROBOCOPY "z:\\temp\\testfiles" ...).....1015 / 4.4 <fastest>
    (RJP-ROBOCOPY "z:\\temp\\testfiles" ...).....1016 / 4.4
    (RJP-XCOPY "z:\\temp\\testfiles" "*....).....4469 / 1
    (RJP-XCOPY "z:\\temp\\testfiles" "*....).....4469 / 1 <slowest>
Edit: I have found out I swapped 2 parameters but strangely the program works the same...
this is the right syntax (rjp-robocopy "z:\\temp\\testfiles" "z:\\temp2\\testfiles" "*.*" "/XD")