Jump to content

Recommended Posts

Posted

Hi. I  use this lisp code to convert the form of a txt file from

 

this

 

0	299153.24	4218596.34
1	299163.21	4218607.51
2	299163.99	4218607.02
3	299176.94	4218599.90
4	299207.37	4218583.27
5	299220.70	4218575.98
6	299242.94	4218563.71
7	299253.41	4218557.75
8	299256.14	4218556.19
9	299253.94	4218553.77
10	299251.66	4218551.25
11	299248.52	4218547.79
12	299244.81	4218549.59
13	299235.68	4218554.00
14	299218.39	4218562.90
15	299197.46	4218573.57
16	299177.72	4218583.74
17	299164.10	4218590.73
18	299153.87	4218596.07
19	299153.24	4218596.34

 

to

299153.24,4218596.34
299163.21,4218607.51
299163.99,4218607.02
299176.94,4218599.90
299207.37,4218583.27
299220.70,4218575.98
299242.94,4218563.71
299253.41,4218557.75
299256.14,4218556.19
299253.94,4218553.77
299251.66,4218551.25
299248.52,4218547.79
299244.81,4218549.59
299235.68,4218554.00
299218.39,4218562.90
299197.46,4218573.57
299177.72,4218583.74
299164.10,4218590.73
299153.87,4218596.07
299153.24,4218596.34

 

The code works fine but convert only one file a time. Is it possible to update the code to convert all *.txt files in the folder to *.pl  at once . The code I use is

 

    (defun c:test (/ FF FFN TXT TXTS)
      (setq ffn  (getfiled "Select file " "" "txt" 0)
            ff   (open ffn "r")
            txts nil
      )
      (while (setq txt (read-line ff)) (setq txts (cons txt txts)))
      (close ff)
      (setq txts (mapcar '(lambda (x) (STD-STRSPLIT x "\t,; ")) (reverse txts)))
      (setq txts
             (mapcar
               '(lambda (x) (vl-remove-if '(lambda (y) (and (not (equal "0" y)) (zerop (atof y)))) x))
               txts
             )
      )
      (setq ff (open (strcat (substr ffn 1 (- (strlen ffn) 3)) "pl") "w"))
      (foreach item txts (write-line (strcat (cadr item) "," (caddr item)) ff))
      (close ff)
      (princ)
    )
     
    ;;; The order of chars in delim is not important.
    ;;; keeping null tokens, not as with std-strtok.
    ;;; Might be renamed to std-string-split
    ;;; by Vladimir Nesterowsky
    (defun STD-STRSPLIT (s delims / len s1 i c lst)
      (setq delims (vl-string->list delims) ; fixed
            len    (strlen s)
            s1     ""
            i      (1+ len)
      )
      (while (> (setq i (1- i)) 0)
        (setq c (substr s i 1))
        (if (member (ascii c) delims)
          (if (/= i len)                    ; "1,2," -> ("1" "2") and not ("1" "2" "")
            (setq lst (cons s1 lst)
                  s1  ""
            )
          )
          (setq s1 (strcat c s1))
        )
      )
      (cons s1 lst)                         ; ",1,2" -> ("" "1" "2")
    )

 

I upload some txt files to test the code

 

 

Thanks

 

 

 

 

 

 

 

 

 

 

1.txt 2.txt 3.txt

Posted

Hi
You simply need to get the list of files in that directory with 'vl-directory-files' and create a loop so the code repeats for each file.

Posted

 

The main function, modified, could be like this:

 

(defun c:test (/ FF FFN TXT TXTS ab d lst rpta)
  (if (setq d (getfiled "Select file " "" "txt" 0))
    (progn
      (while (not (member (setq rpta (strcase (getstring (strcat "\nConvert Only \'" (setq ab (vl-filename-base d)) ".txt\'? <Only>/All directory: ")))) '("" "O" "A")))
	(princ "\n* Invalid option * Try again (ENTER, O or A) ...")
      )
      (if (= rpta "A")
	(setq lst (vl-directory-files (vl-filename-directory d) "*.txt"))
	(setq lst (list (strcat ab ".txt")))
      )
      (foreach ffn lst
	(setq ff (open (setq ffn (strcat (vl-filename-directory d) "\\" ffn)) "r")
	      txts nil
	)
	(while (setq txt (read-line ff))
	  (setq txts (cons txt txts))
	)
	(close ff)
	(setq txts (mapcar '(lambda (x) (STD-STRSPLIT x "\t,; ")) (reverse txts)))
	(setq txts
	       (mapcar
		 '(lambda (x)
		    (vl-remove-if
		      '(lambda (y) (and (not (equal "0" y)) (zerop (atof y))))
		      x
		    )
		  )
		 txts
	       )
	)
	(setq ff (open (strcat (substr ffn 1 (- (strlen ffn) 3)) "pl") "w"))
	(foreach item	txts
	  (write-line (strcat (cadr item) "," (caddr item)) ff)
	)
	(close ff)
      )
      (startapp "explorer" (vl-filename-directory (strcat (vl-filename-directory d) "\\")))
    )
  )
  (princ)
)

 

Posted

PS:

Try it.
I haven't done it.

Posted

I try your code. Ithink it works. I will test more.

 

Thanks GLAVCVS

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