+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19
  1. #1
    Full Member dilan's Avatar
    Discipline
    Civil
    Using
    Civil 3D 2017
    Join Date
    Jan 2018
    Location
    From North
    Posts
    36

    Default Delete files, directory or folder on your computer using Lsp

    Registered forum members do not see this ad.

    Hello everybody!
    On my computer in the Temp folder, I have .txt files.
    I know their names and positions, how can I delete them using lsp?
    I thought that I myself could write a program, but later I understood that I can not manage.
    I started with that first need to get a username:
    Code:
    (setq username (getenv "username"))
    But on this and stopped.... I do not have enough knowledge to finish it ((
    I decided to apply to specialists

  2. #2
    Quantum Mechanic BIGAL's Avatar
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    10,702

    Default

    A batch file is easier and can be ran outside Autocad. Save this to c:\ as deltext.bat just type c:\deltext in the square bottom left as a command in windows. Yes you can run inside Autocad as well.

    Code:
    c:
    cd \temp
    del *.txt
    If their part of a lisp program and your writing say temporary files then use (vl-file-delete filename) but its one file at a time.
    A man who never made mistakes never made anything

  3. #3
    Full Member dilan's Avatar
    Discipline
    Civil
    Using
    Civil 3D 2017
    Join Date
    Jan 2018
    Location
    From North
    Posts
    36

    Default

    Quote Originally Posted by BIGAL View Post
    A batch file is easier and can be ran outside Autocad. Save this to c:\ as deltext.bat just type c:\deltext in the square bottom left as a command in windows. Yes you can run inside Autocad as well.

    Code:
    c:
    cd \temp
    del *.txt
    If their part of a lisp program and your writing say temporary files then use (vl-file-delete filename) but its one file at a time.
    Thank you BIGAL
    I will try to use the second option

  4. #4
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,057

    Default

    Welcome on Cadtutor Dilan


    quickly copied and pasted something together , just for fun and barely tested. But for file operations beautiful tools like Total Commander etc. exists.


    Code:
    
    ; http://www.cadtutor.net/forum/showth...uter-using-Lsp
    ; written by RLX for Dilan 9 jan 2018
    (defun c:Delete_Dilan ( / folder extension file file-list )
      (vl-load-com)
      (if (and (setq extension "*.txt" folder (GetFolder "Select folder with txt files"))
        (vl-consp (setq file-list (vl-directory-files folder extension 1)))
        (vl-consp (setq file-list (Tokkie folder extension file-list file-list)))
        (vl-consp (setq file-list (mapcar '(lambda (x)(strcat folder "\\" x)) file-list))))
        (foreach file file-list (vl-file-delete file))
      )
      (princ)
    )
    ; test (setq fldr (GetFolder "Who , what , Where?"))
    (defun GetFolder ( msg / sh objFolder objParentFolder strPath)
      (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application" ))
      (setq objFolder (vlax-invoke sh 'BrowseForFolder 0 msg 0 ""))
      (if objFolder 
       (and
         (setq strTitle (vlax-get objFolder "Title"))
         (setq objParentFolder (vlax-get objFolder 'ParentFolder))
         (setq strPath (vlax-get (vlax-invoke objParentFolder "Parsename" strTitle) "Path"))
         (vlax-release-object objParentFolder)
         (vlax-release-object objFolder))
       (vlax-release-object sh)
      )
      strPath
    )
    (defun pimp_list ( %lst / i imax slim)
      ; stringlimit (emperical), uppercase is bigger , but so are many M's or W's
      (if (= #RlxBatch-ShowUpperCaseNames "1") (setq slim 82)(setq slim 95))
      (if (or (void RlxBatch-UseTempProjectList) (= RlxBatch-UseTempProjectList "0"))
        (progn
          (setq i 0 imax (strlen (itoa (length %lst))))
          (mapcar '(lambda (x) (setq i (1+ i)) (strcat (fixitoa i imax) " - " (limitstring x slim))) %lst)
        )
        ; this option retains number of position in (main) project list for item in sub project list
        (progn
          (setq imax (strlen (itoa (length RlxBatch-ProjectList))))
          (mapcar
     '(lambda (x) (strcat (fixitoa (1+ (vl-position (strcase x t) RlxBatch-ProjectList)) imax) " - " (limitstring x slim)))
      %lst
          )
        )
      )
    )
    
    
    ; s = string , n = max string length
    (defun limitstring ( s n / l)
      (setq l (strlen s)) (if (> l n) (strcat (substr s 1 (- (/ n 2) 7 )) " . . . " (substr s (- l (/ n 2)))) (strcat s "")))
    
    
    ;check if item is nil of empty string (5 spaces deep)
    (defun void (x) (if (member x (list "" " " "  " "   " "    " "     " nil '())) t nil))
    
    
    ; make sure itoa has fixed length , i.e. (fixitoa 1 3) -> "001"
    (defun fixitoa ( #i #n / s ) (setq s (itoa #i))(while (> #n (strlen s))(setq s (strcat "0" s))) s)
    
    ;--- Tokkie ------------------------------------------------------Tokkie ------------------------------------------------------- Tokkie ---
    ; alternative for listbox multiple selection, a lot of trouble (and not necessarily better) , just different
    ; $txt1 & 2 = header text 1 & 2 , %lst% list of strings, %plist = pre-selected list of strings (if any)
    ; (Tokkie "Block1" "Attributes for visibility2" '("a1" "a2" "a3") '("a2"))
    ; this means toggle for "a1" & "a3" are "0" (off) and "a2" is on ("1")
    ; if previous list is empty all toggles will have default "0" (this differs from my original smartlist routines)
    ; also different is pretty-list , designed to limit string length of members in the list
    (defun Tokkie ( $txt1 $txt2 %lst %plist / err fn fp dcl nof-tog cur-tog pretty-list tokkie-list slider-index return)
      (Tokkie_Init)
      (Tokkie_Dialog)
      (Tokkie_Exit)
      (terpri)
      (reverse return)
    )
    (defun Tokkie_Init ()
      ;(setq err *error* *error* Tokkie_err)
      (cond
        ((not (setq fn (vl-filename-mktemp "Tokkie.dcl"))))
        ((not (setq fp (open  fn "w"))))
        ((null %lst))
        ; tokkie-list keeps toggle value (default "1") for each member in %lst
        (t (setq nof-tog (length %lst) pretty-list (pimp_list %lst)))
      )
      ; tokkie list holds default for toggles - (chr 49) = "1" , (chr 48) = "0"
      (if (and (not (null %lst)) (not (null %plist)))
        ; if item is found in both argument lists , set toggle on ("1") else off ("0")
        (setq tokkie-list (mapcar '(lambda (x)(if (member x %plist)(chr 49)(chr 48))) %lst))
        ; if empty or no preset list was provided all tokkies are off
        (setq tokkie-list (mapcar '(lambda (x) (chr 48)) %lst)))
      
      (if (null %lst)
        (progn (alert "Nothing to show")(Tokkie_Exit))
        (progn
          (Tokkie_write_header)
          (if (<= nof-tog 10)(Tokkie_write_body1 pretty-list)(Tokkie_write_body2 pretty-list))
          (Tokkie_write_footer)
        )
      )
      (if fp (close fp))(gc)
      (setq slider-index 0)
    )
    (defun Tokkie_err (s) (princ s)(Tokkie_Exit)(princ))
    (defun Tokkie_Exit ()
      ;|(setq *error* err)|; (if fp (close fp)) (if dcl (unload_dialog dcl))(if (and fn (findfile fn))(vl-file-delete fn)))
    
    (defun Tokkie_write_header ()
      (write-line
        (strcat
          "Tokkie:dialog{label=\"Tokkie - Rlx(2017)\";spacer;spacer;"
          ":text_part {key=\"txt1\";width=100;fixed_width=true;}"
          ":text_part{key=\"txt2\";width=100;fixed_width=true;}spacer;") fp))
    
    (defun Tokkie_write_body1 ( %l / i )
      (write-line ":boxed_column{" fp)
      (setq i 0)
      (mapcar '(lambda (x)
          (write-line
            (strcat
       ":row{spacer;:toggle{key=\"tg" (itoa i) "\";width=2;fixed_width=true;}"
       ":text_part{key=\"tp" (itoa i) "\";label=\"" x "\";width=100;fixed_width=true;}}") fp)
          (setq i (1+ i)))
       %l
      )
      (repeat (- 10 i)(write-line ":row{height=1.5;}" fp))
      (write-line "}spacer;" fp)
    )
    (defun Tokkie_write_body2 ( %l / i )
      (write-line ":boxed_row {:column{" fp)
      (setq i 0)
      (repeat 10
        (write-line
          (strcat
     ":row { spacer; :toggle { key =\"tg" (itoa i) "\";width=2;fixed_width=true;}"
     ":text_part{key=\"tp" (itoa i) "\";label=\"" (nth i %l) "\";width=100;fixed_width=true;}}") fp)
        (setq i (1+ i))
      )
      (write-line
        (strcat "}:column{:slider{key=\"sldr\";layout=vertical;min_value="(itoa (- 0 nof-tog)) ";max_value=0" 
         ";small_increment=1;big_increment=10;value=0;}}}spacer;") fp)
    )
    (defun Tokkie_write_footer ()
      (write-line
        (strcat
          "spacer;"
          ": row {: button{label=\"Select All\";key=\"all\";}:button{label=\"Select None\";key=\"none\";}}"
          "spacer;ok_cancel;}") fp))
    (defun Tokkie_Dialog ( / n drv inp)
      (if (and (setq n 0 dcl (load_dialog fn)) (new_dialog "Tokkie" dcl))
        (progn
          (Tokkie_DialogUpdate)
          (Tokkie_DialogActions)
          (setq drv (start_dialog))
          (if (= drv 1)
     (mapcar '(lambda (x y) (if (= x "1")(setq return (cons y return)))) tokkie-list %lst)
     (setq return nil))
        )
      )
    )
    (defun Tokkie_DialogUpdate ( / i )
      (if (= (type $txt1) 'STR)(set_tile "txt1" $txt1))
      (if (= (type $txt2) 'STR)(set_tile "txt2" $txt2))
      (set_tile "sldr" (itoa slider-index))
      (update_tokkies)
    )
    (defun Tokkie_DialogActions ()
      (repeat 10 (action_tile (strcat "tg" (itoa n)) (strcat "(toggle_me $value " (itoa n) ")" ))(setq n (1+ n)))
      (action_tile "sldr" "(update_slider $value)")
      (action_tile "all" "(Tokkie_SelectAll)")
      (action_tile "none" "(Tokkie_SelectNone)")
      (action_tile "ok" "(done_dialog 1)")
      (action_tile "cancel" "(done_dialog 0)")
    )
    (defun update_slider ( #i ) (setq slider-index (atoi #i)) (Tokkie_DialogUpdate))
    (defun update_tokkies (/ tokkie-index tokkie-name i)
      ;max number of toggles in dialog is 10
      (if (> (abs slider-index) (- nof-tog 10))
        (setq tokkie-index (- nof-tog 10))(setq tokkie-index (abs slider-index)))
      (if (< tokkie-index 0)(setq tokkie-index 0))
      (setq i 0)
      (while (and (< i 10) (setq tokkie-name (nth tokkie-index pretty-list)))
        (set_tile (strcat "tp" (itoa i)) tokkie-name)
        (set_tile (strcat "tg" (itoa i)) (nth tokkie-index tokkie-list))
        (setq i (1+ i) tokkie-index (1+ tokkie-index))
      )
    )
      
    (defun toggle_me ( $v #i / idx )
      (if (> (abs slider-index) (- nof-tog 10))(setq idx (- nof-tog 10))(setq idx (abs slider-index)))
      (setq tokkie-list (nthSubst (+ #i (if (< idx 0) 0 idx)) $v tokkie-list))
    )
    (defun Tokkie_SelectAll ();reset all toggles to "1"
      ;(setq tokkie-list (mapcar '(lambda (x) (chr 49)) %lst))(Tokkie_DialogUpdate))
      (setq tokkie-list (mapcar '(lambda (x) (chr 49)) pretty-list))(Tokkie_DialogUpdate))
    (defun Tokkie_SelectNone ();reset all toggles to "0"
      ;(setq tokkie-list (mapcar '(lambda (x) (chr 48)) %lst))(Tokkie_DialogUpdate))
      (setq tokkie-list (mapcar '(lambda (x) (chr 48)) pretty-list))(Tokkie_DialogUpdate))
    (defun nthSubst (i x l / n) (setq n -1)(mapcar '(lambda (y) (if (eq i (setq n (1+ n))) x y )) l ))
    ;--- Tokkie ------------------------------------------------------Tokkie ------------------------------------------------------- Tokkie ---
    (c:Delete_Dilan)
    gr. Rlx


    ps.pps. edited code to include subfuction limitstring, fixitoa & void , part of my lib with little subfunctions so I forgot it the first time , just noticed it after I posted
    Last edited by rlx; 9th Jan 2018 at 12:41 pm.

  5. #5
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    889

    Default

    Try this:
    Code:
    (defun c:foo (/ tmp)
      ;; Deletes "*.txt" files in temp dir!
      (foreach x (vl-directory-files (setq tmp (getenv "temp")) "*.txt")
        (vl-file-delete (strcat tmp "\\" x))
        (print (strcat "DELETING >> " tmp "\\" x))
      )
      (princ)
    )(vl-load-com)

  6. #6
    Full Member dilan's Avatar
    Discipline
    Civil
    Using
    Civil 3D 2017
    Join Date
    Jan 2018
    Location
    From North
    Posts
    36

    Default

    Quote Originally Posted by ronjonp View Post
    Try this:
    Code:
    (defun c:foo (/ tmp)
      ;; Deletes "*.txt" files in temp dir!
      (foreach x (vl-directory-files (setq tmp (getenv "temp")) "*.txt")
        (vl-file-delete (strcat tmp "\\" x))
        (print (strcat "DELETING >> " tmp "\\" x))
      )
      (princ)
    )(vl-load-com)
    Thank you.This is what you need. How can I do not to delete all the files .txt in the "Temp" folder? For example I need to delete the file 123.txt and 321.txt...

  7. #7
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,474

    Default

    Rlx, theres a tiny issue in the GetFolder subfunction you used - shell application object won't be released if the user specifies the folder.
    On the other hand I think (think?,more like studied from the pros) that those "external acad objects" must be released at all cost (which would mean error-trapping/wrapping).
    So heres a more reliable approach that you can use:

    Code:
    ; msg - [STR] - optional prompt message
    ; defpath - [STR] - optional default path
    ; newfolderopt [T/nil] - T = display option to create new folder, else don't
    ; Examples: (GetFolder nil nil nil) ; (GetFolder "Hello world" "D:\\GAMES" T)
    (defun GetFolder ( msg defpath newfolderopt / shell folder parentfolder path )
      (vl-catch-all-apply 
        (function
          (lambda nil
            (setq shell (vlax-get-or-create-object "Shell.Application"))
            (setq folder (vlax-invoke-method shell 'BrowseForFolder 0 (cond (msg)("")) (if newfolderopt 0 512) (cond (defpath) ((strcat (getenv "userprofile") "\\Desktop\\")))))
            (setq parentfolder (vlax-get-property folder 'ParentFolder))
            (setq path (vlax-get-property (vlax-invoke-method parentfolder 'Parsename (vlax-get-property folder 'Title)) 'Path))
          )
        )
      )
      (foreach x (reverse (list shell folder parentfolder path)) (vl-catch-all-apply 'vlax-release-object (list x))) path
    ); defun GetFolder

  8. #8
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,591
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  9. #9
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,474

    Default

    Quote Originally Posted by Lee Mac View Post
    Well thats the perfect approach, I just wanted to use my gray matter a bit.

  10. #10
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,057

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Grrr View Post
    Rlx, theres a tiny issue in the GetFolder subfunction you used - shell application object won't be released if the user specifies the folder.
    On the other hand I think (think?,more like studied from the pros) that those "external acad objects" must be released at all cost (which would mean error-trapping/wrapping).
    So heres a more reliable approach that you can use:

    Code:
    ; msg - [STR] - optional prompt message
    ; defpath - [STR] - optional default path
    ; newfolderopt [T/nil] - T = display option to create new folder, else don't
    ; Examples: (GetFolder nil nil nil) ; (GetFolder "Hello world" "D:\\GAMES" T)
    (defun GetFolder ( msg defpath newfolderopt / shell folder parentfolder path )
      (vl-catch-all-apply 
        (function
          (lambda nil
            (setq shell (vlax-get-or-create-object "Shell.Application"))
            (setq folder (vlax-invoke-method shell 'BrowseForFolder 0 (cond (msg)("")) (if newfolderopt 0 512) (cond (defpath) ((strcat (getenv "userprofile") "\\Desktop\\")))))
            (setq parentfolder (vlax-get-property folder 'ParentFolder))
            (setq path (vlax-get-property (vlax-invoke-method parentfolder 'Parsename (vlax-get-property folder 'Title)) 'Path))
          )
        )
      )
      (foreach x (reverse (list shell folder parentfolder path)) (vl-catch-all-apply 'vlax-release-object (list x))) path
    ); defun GetFolder

    Just quickly cut some code from some of my appies so it may not have been the best ones. On the other hand , it took me about two minutes... but you're right have to clean up or out some of my old stuf. Thanx for the update and the advice , allways appreciated and never too old to learn







    gr. Rlx

Similar Threads

  1. Replies: 5
    Last Post: 25th Jun 2014, 05:20 pm
  2. LISP Folder Directory and Hyperlink
    By Ohnoto in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 29th Sep 2013, 01:50 pm
  3. Using vl-directory-files to populate list box
    By lfe011969 in forum AutoLISP, Visual LISP & DCL
    Replies: 15
    Last Post: 11th Aug 2010, 07:30 pm
  4. How to copy a list of files from one directory to another?
    By MarcoW in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 7th Mar 2010, 03:37 pm
  5. Opening Files from same directory?
    By Ste1978 in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 31st May 2006, 12:51 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts