Author Topic: Can a file be moved using LISP...  (Read 6130 times)

0 Members and 1 Guest are viewing this topic.

A_LOTA_NOTA

  • Guest
Can a file be moved using LISP...
« on: April 12, 2011, 04:33:46 PM »
When a drawing has been revised we create a PDF and move the old PDF to a "VOID" folder. So for example when 301s1r1.pdf is created 301s1r0.pdf would need to be moved. Could lisp be used to accomplish the moving of the old file?

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can a file be moved using LISP...
« Reply #1 on: April 12, 2011, 04:50:26 PM »
Maybe something like this?

Code: [Select]
(defun LM:MoveFile ( file dest / fso result ) (vl-load-com)
  (if
    (and
      (setq file (findfile file))
      (setq dest (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dest)) "\\"))
      (or (vl-file-directory-p dest) (vl-mkdir dest))
      (setq fso  (vlax-create-object "Scripting.FileSystemObject"))
    )
    (setq result (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke (list fso 'MoveFile file dest)))))
  )
  (if fso (vlax-release-object  fso))
  result
)

e.g.

Code: [Select]
(LM:MoveFile "C:\\MyFile.txt" "C:\\MyFolder\\")
MyFolder is created if non-existent.
« Last Edit: April 12, 2011, 04:54:40 PM by Lee Mac »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Can a file be moved using LISP...
« Reply #2 on: April 12, 2011, 04:52:36 PM »
Quote
MoveFile
Bah! I tried Move, it didn't work and I went on with my business.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

LE3

  • Guest
Re: Can a file be moved using LISP...
« Reply #3 on: April 12, 2011, 04:58:38 PM »
or the old fashion way (of DOS) via command SHELL

Command: SH
OS Command: MOVE E:\\TEST.TXT E:\\TEMP

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can a file be moved using LISP...
« Reply #4 on: April 12, 2011, 05:01:06 PM »
Quote
MoveFile
Bah! I tried Move, it didn't work and I went on with my business.

The 'Move' method may also be used, but it requires different parameters:

Code: [Select]
(defun LM:MoveFile2 ( file dest / fso fob result ) (vl-load-com)
  (if
    (and
      (setq file (findfile file))
      (setq dest (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dest)) "\\"))
      (or (vl-file-directory-p dest) (vl-mkdir dest))
      (setq fso  (vlax-create-object "Scripting.FileSystemObject"))
      (setq fob  (vlax-invoke fso 'getfile file))
    )
    (setq result (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke (list fob 'Move dest)))))
  )
  (if fso (vlax-release-object  fso))
  (if fob (vlax-release-object  fob))
  result
)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can a file be moved using LISP...
« Reply #5 on: April 12, 2011, 05:12:40 PM »
Or for moving files and folders:

Code: [Select]
(defun LM:Move ( target dest / fso fob result ) (vl-load-com)
  (if
    (and
      (setq target (findfile target))     
      (setq dest   (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dest)) "\\"))
      (or (vl-file-directory-p dest) (vl-mkdir dest))
      (setq fso    (vlax-create-object "Scripting.FileSystemObject"))
      (setq fob    (vlax-invoke fso (if (vl-file-directory-p target) 'getfolder 'getfile) target))
    )
    (setq result (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke (list fob 'Move dest)))))
  )
  (if fso (vlax-release-object  fso))
  (if fob (vlax-release-object  fob))
  result
)

Code: [Select]
(LM:Move "C:\\MyFile.txt" "C:\\MyFolder\\")

(LM:Move "C:\\NewFolder" "C:\\MyFolder\\")

LE3

  • Guest
Re: Can a file be moved using LISP...
« Reply #6 on: April 12, 2011, 05:22:12 PM »
something so easy like this:
Code: [Select]
(setq OriginalNamePath "E:\\TEST.TXT")
(setq NewNamePath "E:\\TEMP")
;; or change the name too
(setq NewNamePath "E:\\TEMP\\TEST1.TXT")
(defun MoveMyFile(OriginalNamePath NewNamePath)
    (command "SHELL" (strcat "MOVE " OriginalNamePath " " NewNamePath)))

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Can a file be moved using LISP...
« Reply #7 on: April 13, 2011, 03:21:04 AM »
yep
Code: [Select]
(vl-file-rename old-fileorfoldername new-fileorfoldername)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can a file be moved using LISP...
« Reply #8 on: April 13, 2011, 08:11:24 AM »
yep
Code: [Select]
(vl-file-rename old-fileorfoldername new-fileorfoldername)

lol, Trust you to find the easy way  :evil:

Oh well, at least I got some practice with the FSO  8-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Can a file be moved using LISP...
« Reply #9 on: April 13, 2011, 08:12:43 AM »
yep
Code: [Select]
(vl-file-rename old-fileorfoldername new-fileorfoldername)
Nicely done. :kewl:
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

pBe

  • Bull Frog
  • Posts: 402
Re: Can a file be moved using LISP...
« Reply #10 on: April 13, 2011, 08:50:02 AM »
Another.... CMD/DOS

Code: [Select]
(defun MoveDos ( File Fldr)
      (startapp "CMD /c" (strcat (vl-prin1-to-string "move /y ") (vl-princ-to-string File) " "
        (vl-princ-to-string Fldr)))
        )

(MoveDos "D:\\File.txt" "D:\\Folder\\")

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Can a file be moved using LISP...
« Reply #11 on: April 13, 2011, 08:51:40 AM »
Lee, would you mind posting the direct link to the documentation you were reading for FSO?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Can a file be moved using LISP...
« Reply #12 on: April 13, 2011, 08:54:42 AM »
Lee, would you mind posting the direct link to the documentation you were reading for FSO?

Sure,

http://msdn.microsoft.com/en-us/library/kxtftw67%28v=VS.85%29.aspx

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Can a file be moved using LISP...
« Reply #13 on: April 13, 2011, 08:55:51 AM »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: Can a file be moved using LISP...
« Reply #14 on: April 13, 2011, 04:53:51 PM »
I didn't break it, I swear! ...ok, I broke it.