Author Topic: string substitution  (Read 6849 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
string substitution
« on: November 10, 2005, 12:41:06 PM »
How would one convert (in lisp) a string like:

"O:\\J2\\Gilbert\\Park P\\ASI 11-10-05\\"

to

"O:-J2-Gilbert-Park P-ASI 11-10-05-"

basically replace all \\ with - or some other character..

I took a look @ vl-string-subst but apparently it only replaces the first occurence.

Thanks,

Ron


Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: string substitution
« Reply #1 on: November 10, 2005, 12:44:46 PM »
Perhaps --

Code: [Select]
(defun Replace ( oldText newText text / i )
    (if (/= oldText newText)
        (while (setq i (vl-string-search oldText text))
            (setq text
                (vl-string-subst
                    newText
                    oldText
                    text
                    i
                )
            )
        )
    )
    text
)

(replace "\\" "-" OriginalString)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: string substitution
« Reply #2 on: November 10, 2005, 12:51:10 PM »
Look at vl-string-translate.

Tim
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Fatty

  • Guest
Re: string substitution
« Reply #3 on: November 10, 2005, 12:52:49 PM »
Hi Ron

Is there you want?

(vl-string-translate "\\\"" "-" "O:\\J2\\Gilbert\\Park P\\ASI 11-10-05\\")

Thank you

Fatty

ronjonp

  • Needs a day job
  • Posts: 7529
Re: string substitution
« Reply #4 on: November 10, 2005, 01:16:36 PM »
vl-string-translate was it! Damn thing was right in front of me :oops:.

Thanks all.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Andrea

  • Water Moccasin
  • Posts: 2372
Re: string substitution
« Reply #5 on: November 14, 2005, 07:49:34 PM »
there is anothe way to do it..


(setq L1 "O:\\J2\\Gilbert\\Park P\\ASI 11-10-05\\")
(while (vl-string-search "\\" L1)
(setq L1 (vl-string-subst "-" "\\" L1))
)


Just let you know.. ^-^
Keep smile...

ronjonp

  • Needs a day job
  • Posts: 7529
Re: string substitution
« Reply #6 on: November 15, 2005, 09:58:35 AM »
Thanks Andrea.
I ended up using this:

Code: [Select]
(setq dwgpath (strcat (vl-string-right-trim "_" (substr (vl-string-translate "\\\"" "_" (getvar 'dwgprefix))4))"/"))
Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string substitution
« Reply #7 on: November 17, 2005, 06:04:59 PM »
Thanks Andrea.
I ended up using this:

Code: [Select]
(setq dwgpath (strcat (vl-string-right-trim "_" (substr (vl-string-translate "\\\"" "_" (getvar 'dwgprefix))4))"/"))
Ron

Hey, Whats wrong with this?
Code: [Select]
(setq dwgpath (vl-string-trim "C:_" (vl-string-translate "\\\"" "_" (getvar 'dwgprefix))))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string substitution
« Reply #8 on: November 17, 2005, 06:09:51 PM »
Never mind,  :-(
If the wanted string begins or ends with C it too will be removed.
Back to the drawing board.

PS this did it.
Code: [Select]
(setq dwgpath (vl-string-trim "_" (vl-string-translate "\\\"" "_" (vl-string-trim "C:"(getvar 'dwgprefix)))))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: string substitution
« Reply #9 on: November 17, 2005, 06:21:00 PM »
Hey Cab,

I'm using this for backup purposes. The folder the drawing resides in get replicated on my C: drive in this manner:



The drive part I am trimming varies so C:\ won't work all the time:

Here is the code I'm using that runs when a drawing is saved:

Code: [Select]
(defun copydwg (/ destdir dwgdir *date* hour dwgpath bkupdir)
(if (not (wcmatch (getvar 'DWGNAME) "Drawing*.dwg"))
  (progn
   (defun *date* (/ curdate curtime)
     (setq   curdate (rtos (fix (getvar 'cdate)) 2 0)
      curtime (strcat (substr curdate 5 2) "-" (substr curdate 7 2) "-" (substr curdate 1 4)"/"))
   )
(defun hour (/ ctime stime)
  (setq ctime (rtos (rem (getvar 'cdate) 1) 2 6)
        stime (strcat (substr ctime 3 2)"/"))
)
(setq bkupdir "C:/AutoCAD-BAK/")
(setq dwgpath (strcat (vl-string-right-trim "_" (substr (vl-string-translate "\\\"" "_" (getvar 'dwgprefix))4))"/"))

;Makes C:\AutoCAD-BAK\curdate\curhour\curdwgpath if it doesn't already exist
   (if (not
       (vl-file-directory-p bkupdir))
       (vl-mkdir bkupdir)
   )
   (if (not
       (vl-file-directory-p (strcat bkupdir (*date*))))
       (vl-mkdir (strcat bkupdir (*date*)))
   )
   (if (not
       (vl-file-directory-p (strcat bkupdir (*date*)(hour))))
       (vl-mkdir (strcat bkupdir (*date*)(hour)))
   )
   (if (not
       (vl-file-directory-p (strcat bkupdir (*date*)(hour) dwgpath)))
       (vl-mkdir (strcat bkupdir (*date*)(hour) dwgpath))
   )

;For some reason the files get huge if not deleted then saved on top of
   (if (findfile (strcat bkupdir (*date*)(hour) dwgpath (getvar 'dwgname)))
     (vl-file-delete (strcat bkupdir (*date*)(hour) dwgpath (getvar 'dwgname)))
   )
   (setq destdir (strcat bkupdir (*date*)(hour) dwgpath (getvar 'dwgname))
         dwgdir  (strcat (getvar 'dwgprefix) (getvar 'dwgname))
   )
   (vl-file-copy dwgdir destdir T)
      )
    )
  )

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string substitution
« Reply #10 on: November 17, 2005, 07:15:42 PM »
Ron,
Looks like you have it buttoned up.
Very nice.

BTW here is another way to code this section.
Just a FYI.
Code: [Select]
;Makes C:\AutoCAD-BAK\curdate\curhour\curdwgpath if it doesn't already exist
      (or (vl-file-directory-p bkupdir)
          (vl-mkdir bkupdir)
      )
      (or (vl-file-directory-p (strcat bkupdir (*date*)))
          (vl-mkdir (strcat bkupdir (*date*)))
      )
      (or (vl-file-directory-p (strcat bkupdir (*date*) (hour)))
          (vl-mkdir (strcat bkupdir (*date*) (hour)))
      )
      (or (vl-file-directory-p (strcat bkupdir (*date*) (hour) dwgpath))
          (vl-mkdir (strcat bkupdir (*date*) (hour) dwgpath))
      )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: string substitution
« Reply #11 on: November 18, 2005, 09:47:01 AM »
Thanks for the tip CAB :).

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string substitution
« Reply #12 on: November 18, 2005, 10:02:26 AM »
BTW
Very cool graphics.
What program did you use to create the animation?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: string substitution
« Reply #13 on: November 18, 2005, 11:17:00 AM »
http://www.techsmith.com/

Very cool little application. Well worth the money.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC