TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: piffling on July 26, 2012, 06:53:48 PM

Title: Date Format
Post by: piffling on July 26, 2012, 06:53:48 PM
I have a .txt file list of single line entries with a date on each entry in dd-mm-yyyy format and I want to change the format to mm-dd-yyyy.
Anyone have a lisp that will do that?
Title: Re: Date Format
Post by: DEVITG on July 26, 2012, 10:54:21 PM
Please upload the TXT file
Title: Re: Date Format
Post by: BlackBox on July 27, 2012, 12:36:27 AM
Why don't you just pass the string returned by each 'Read-Line' call, to a parser sub-function, which returns a list of sub-strings:

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun RM:Parser (char string / i segments segment)
  3.   ;; RenderMan, TheSwamp.org
  4.   ;; Example: (RM:Parser "-" "dd-mm-yyyy")
  5.   ;; Returns: ("dd" "mm" "yyyy")
  6.   (while (setq i (vl-string-search char string))
  7.     (setq segments (cons (setq segment (substr string 1 i)) segments))
  8.     (setq string (substr string (+ 2 i)))
  9.   )
  10.   (reverse (cons string segments))
  11. )
  12.  

... Then Strcat the list of strings in the desired order:

Code - Auto/Visual Lisp: [Select]
  1. (defun RM:Reorder (lst)
  2.   ;; RenderMan, TheSwamp.org
  3.   ;; Example: (RM:Reorder (RM:Parser "-" "dd-mm-yyyy"))
  4.   ;; Returns: "mm-dd-yyyy"
  5.   (strcat (cadr lst) "-" (car lst) "-" (last lst))
  6. )
  7.  

** Note - The RM:Reorder sub-function is dependent on the specific string format you described in the OP, and must be revised to suite any alternative string format.



Quick example:
Code - Auto/Visual Lisp: [Select]
  1. (foreach string '("10-01-2012" "15-02-2012" "12-03-2012" "17-04-2012")
  2.   (print (RM:Reorder (RM:Parser "-" string)))
  3. )
  4.  

Console output:
Code - Auto/Visual Lisp: [Select]
  1. "01-10-2012"
  2. "02-15-2012"
  3. "03-12-2012"
  4. "04-17-2012"
  5.  
Title: Re: Date Format
Post by: Lee Mac on July 27, 2012, 07:14:37 AM
Assumes format of Text file is exactly as specified:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dateswitch ( / d1 d2 f1 f2 ln )
  2.     (if (and
  3.             (setq f1 (getfiled "" "" "txt" 16))
  4.             (setq d1 (open f1 "r"))
  5.             (setq f2 (fnsplitl f1)
  6.                   f2 (strcat (car f2) (cadr f2) "_modified.txt")
  7.                   d2 (open f2 "w")
  8.             )
  9.         )
  10.         (progn
  11.             (while (setq ln (read-line d1))
  12.                 (write-line (strcat (substr ln 4 2) "-" (substr ln 1 2) (substr ln 6)) d2)
  13.             )
  14.             (close d1)
  15.             (close d2)
  16.             (startapp "notepad" f2)
  17.         )
  18.     )
  19.     (princ)
  20. )
Title: Re: Date Format
Post by: BlackBox on July 27, 2012, 08:19:38 AM
I had to Google it (fnsplitl), and I couldn't agree with Hippe013 more :-D :

Quote from: Hippe013
Lee you are a dictionary of undocumented lisp functions! :D

linky (http://www.cadtutor.net/forum/showthread.php?51028-Grabbing-File-Name&p=346645&viewfull=1#post346645)
Title: Re: Date Format
Post by: Lee Mac on July 27, 2012, 08:32:18 AM
 :lol:
Title: Re: Date Format
Post by: piffling on July 27, 2012, 09:39:19 AM
RenderMan,
I sorry you went to all that trouble for me,  but unfortunately, I don't have clue what you are trying to tell me.
Thanks for your efforts, I'm sure it will help someone else.

Lee Mac,
I failed to mention in my request that the date is last on each line.
Your lisp works perfectly when I move the date to the beginning of each line, which is no big deal. I get some real funny results otherwise.
Thanks much.

By the bye, why are the lines of code numbered? They have to be removed to run the lisp, don't they?
Title: Re: Date Format
Post by: Lee Mac on July 27, 2012, 10:29:13 AM
Lee Mac,
I failed to mention in my request that the date is last on each line.
Your lisp works perfectly when I move the date to the beginning of each line, which is no big deal. I get some real funny results otherwise.

As I stated:

Assumes format of Text file is exactly as specified

The more information you provide (within reason), the less time is wasted trying to ascertain what you really require.

By the bye, why are the lines of code numbered? They have to be removed to run the lisp, don't they?

The line numbers are automatically displayed by the engine that formats the code in the post (geshi), these should not be selectable when you copy the code.
Title: Re: Date Format
Post by: BlackBox on July 27, 2012, 10:59:30 AM
RenderMan,
I sorry you went to all that trouble for me,  but unfortunately, I don't have clue what you are trying to tell me.
Thanks for your efforts, I'm sure it will help someone else.

No worries; we all start somewhere.  :-)
Title: Re: Date Format
Post by: CAB on July 27, 2012, 02:57:54 PM
It's been around for a long time.  8-)
http://www.theswamp.org/index.php?topic=785.0
Title: Re: Date Format
Post by: BlackBox on July 27, 2012, 02:59:52 PM
It's been around for a long time.  8-)
http://www.theswamp.org/index.php?topic=785.0

Isn't that the truth, posted February 05, 2004:

Just stumpled upon a function in A2K4 that's been undocumented since R14.

 :lol:
Title: Re: Date Format
Post by: piffling on July 28, 2012, 10:28:19 PM


As I stated:

Assumes format of Text file is exactly as specified

The more information you provide (within reason), the less time is wasted trying to ascertain what you really require.


Yes, it was my mistake for assuming that if a lisp existed to change the date format, it would find only the strings dd-mm-yyyy and change the order to mm-dd-yyyy.