Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: code help adding date to file name

  1. #1
    I could stop if I wanted to cadd4la's Avatar
    Join Date
    2001-12
    Location
    Newport Beach, CA
    Posts
    399
    Login to Give a bone
    0

    Default code help adding date to file name

    Hello,
    Can someone please help me change this code so the outcome is either 1st choice "drawing name_18 0328.dwg", 2nd choice "drawing name_18 0328.134800.dwg", or will settle for this"drawing name_2018 0328.134800.dwg" (The red text is the area of the code that I'm looking to change), it now gives me this "drawing name-20180328.134800.dwg".

    Code:
     (setq 
          cFNCurrVer (strcat (car (parse (getvar "dwgname") ".")) "_" (STD-STRING-RIGHT-PAD-CHAR (rtos (getvar "CDATE") 2 6) nMaxDateStrLen "0"))
          cSubdFN (strcat (getvar "dwgprefix") cSubd "\\" cFNCurrVer ".dwg")
       )
    Thanks,

    Cadd4la

  2. #2
    100 Club matt.worland's Avatar
    Join Date
    2015-12
    Location
    Denver, CO USA
    Posts
    174
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    You should be able to play with the substr function to break out different parts of the cdate var.

    (setq fullDate (rtos(getvar "cdate") 2 0))
    (setq fileName (strcat (vl-filename-base (getvar "dwgname")) "_" (substr fullDate 3 2) " " (substr fullDate 5 2) (substr fullDate 7 2)(vl-filename-extension (getvar "dwgname"))))
    matt worland

  3. #3
    I could stop if I wanted to cadd4la's Avatar
    Join Date
    2001-12
    Location
    Newport Beach, CA
    Posts
    399
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by matt.worland View Post
    You should be able to play with the substr function to break out different parts of the cdate var.

    (setq fullDate (rtos(getvar "cdate") 2 0))
    (setq fileName (strcat (vl-filename-base (getvar "dwgname")) "_" (substr fullDate 3 2) " " (substr fullDate 5 2) (substr fullDate 7 2)(vl-filename-extension (getvar "dwgname"))))
    Matt, thanks but unfortunately your code doesn't work when I place into the full program. cSubdFN is also used in other parts of the code.

    Regards,

  4. #4
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by cadd4la View Post
    Matt, thanks but unfortunately your code doesn't work when I place into the full program. cSubdFN is also used in other parts of the code.

    Regards,
    As Mat's code returned exactly what you asked for all you need to do is substitute cSubdFN for fileName in his code. As parse isn't a lisp function I have no idea how you got your code to return anything.

  5. #5
    I could stop if I wanted to cadd4la's Avatar
    Join Date
    2001-12
    Location
    Newport Beach, CA
    Posts
    399
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Tom,

    Thanks for your input but it still will not run for me.

    Regards,

  6. #6
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by cadd4la View Post
    Tom,

    Thanks for your input but it still will not run for me.

    Regards,
    The part Mat posted works great, to fix the rest of your code we would have to see it. It needs to include the definition of the parse function used in the code you posted. Guessing that's why Mat started from scratch to code what you were after.

  7. #7
    I could stop if I wanted to cadd4la's Avatar
    Join Date
    2001-12
    Location
    Newport Beach, CA
    Posts
    399
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by Tom Beauford View Post
    The part Mat posted works great, to fix the rest of your code we would have to see it. It needs to include the definition of the parse function used in the code you posted. Guessing that's why Mat started from scratch to code what you were after.
    Tom,

    Here is the program that i'm starting with.
    (defun c:SAV ( / nChkForDirs nChkForFiles cFNCurrVer cPadZero cSubd cSubdFN cSrcFN c_OldDir nVarEXPERT nMaxDateStrLen)
    (setq cSubd "_Old")
    (setq cSrcFN (strcat (getvar "dwgprefix") (getvar "dwgname")))
    (setq nMaxDateStrLen 15)
    (setq nVarEXPERT (getvar "expert"))
    (setvar "expert" 5)
    ; vl-directory-files options
    (setq nChkForDirs -1 nChkForFiles 1)

    ; Make the Filename
    (setq
    cFNCurrVer (strcat (car (parse (getvar "dwgname") ".")) "_" (STD-STRING-RIGHT-PAD-CHAR (rtos (getvar "CDATE") 2 6) nMaxDateStrLen "0"))
    cSubdFN (strcat (getvar "dwgprefix") cSubd "\\" cFNCurrVer ".dwg")
    )

    ; Save the dwg and copy it.
    (prompt "\nSaving...")
    (command ".save" "")

    ; Make the subdir
    (MkSubDir cSubd)
    (prompt (strcat "\nCopying to " cSubdFN))
    (if (not (vl-file-copy cSrcFN cSubdFN))
    (prompt (strcat "\nError copying " cSubdFN)))
    (setvar "expert" nVarEXPERT)
    (princ)
    )

    ; Utility functions
    (defun STD-STRING-RIGHT-PAD-CHAR (s n char)
    (while (< (strlen s) n) (setq s (strcat s char)))
    ;;(substr s 1 n)
    s
    )

    (defun MkSubDir (cDirName / cDir)
    (setq cDir (vl-directory-files "" cDirName -1))
    (if (not cDir)
    (progn
    (vl-mkdir (strcat ".\\" cDirName))
    (vl-directory-files "" cDirName -1)))
    )

    (defun parse (str delim / out pos)
    (setq delim (ascii delim))
    (while (setq pos (vl-string-position delim str))
    ;(if (> pos 0);not just adjacent delimiters
    (setq out (cons (substr str 1 pos) out))
    (setq str (substr str (+ 2 pos)))
    )
    ;(if (> (strlen str) 0);not a trailing delimiter
    (reverse (cons str out))
    ;(reverse out)
    )

  8. #8
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Try:
    Code:
    (defun c:SAV ( /  cSubd cSrcFN  nVarEXPERT fullDate cSubdFN)
      (setq cSubd "_Old"
    		cSrcFN (strcat (getvar "dwgprefix") (getvar "dwgname"))
    		nVarEXPERT (getvar "expert")
    		fullDate (rtos(getvar "cdate") 2 0)
    		cSubdFN (strcat (getvar "dwgprefix") cSubd "\\" (vl-filename-base (getvar "dwgname")) "_" (substr fullDate 3 2) " " (substr fullDate 5 2) (substr fullDate 7 2) (vl-filename-extension (getvar "dwgname")))
      )
      (setvar "expert" 5)
      ; vl-directory-files options
    
      ; Save the dwg and copy it.
      (prompt "\nSaving...")
      (command ".save" "")
    
      ; Make the subdir
      (vl-mkdir (strcat (getvar "dwgprefix") cSubd))
      (prompt (strcat "\nCopying to " cSubdFN))
      (if (not (vl-file-copy cSrcFN cSubdFN))
      (prompt (strcat "\nError copying " cSubdFN)))
      (setvar "expert" nVarEXPERT)
      (princ)
    )
    Last edited by Tom Beauford; 2018-03-30 at 09:27 PM.

  9. #9
    I could stop if I wanted to cadd4la's Avatar
    Join Date
    2001-12
    Location
    Newport Beach, CA
    Posts
    399
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by Tom Beauford View Post
    Try:
    Code:
    (defun c:SAV ( /  cSubd cSrcFN  nVarEXPERT fullDate cSubdFN MkSubDir)
      (setq cSubd "_Old"
    		cSrcFN (strcat (getvar "dwgprefix") (getvar "dwgname"))
    		nVarEXPERT (getvar "expert")
    		fullDate (rtos(getvar "cdate") 2 0)
    		cSubdFN (strcat (getvar "dwgprefix") cSubd "\\" (vl-filename-base (getvar "dwgname")) "_" (substr fullDate 3 2) " " (substr fullDate 5 2) (substr fullDate 7 2) (vl-filename-extension (getvar "dwgname")))
      )
      (setvar "expert" 5)
      ; vl-directory-files options
    
      ; Save the dwg and copy it.
      (prompt "\nSaving...")
      (command ".save" "")
    
      (defun MkSubDir (cDirName / cDir)
    	(setq cDir (vl-directory-files "" cDirName -1))
    	(if (not cDir)
    	  (progn
    		(vl-mkdir (strcat ".\\" cDirName))
    		(vl-directory-files "" cDirName -1))
    	  )
      )
    
      ; Make the subdir
      (MkSubDir cSubd)
      (prompt (strcat "\nCopying to " cSubdFN))
      (if (not (vl-file-copy cSrcFN cSubdFN))
      (prompt (strcat "\nError copying " cSubdFN)))
      (setvar "expert" nVarEXPERT)
      (princ)
    )
    Tom,

    Thank you for your help. The code gives me the format that I want on the file name but will not work unless I already have a folder named _Old in the same folder as the drawing.

    Regards,

  10. #10
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: code help adding date to file name

    Quote Originally Posted by cadd4la View Post
    Tom,

    Thank you for your help. The code gives me the format that I want on the file name but will not work unless I already have a folder named _Old in the same folder as the drawing.

    Regards,
    The only thing I hadn't changed before was the function for adding that folder. I updated the code in my previous post removing your MkSubDir function and simplifying the code. That MkSubDir function tested for any subfolder and only adding one if there wasn't one. So it worked if there wasn't one, but not if there was one. You shouldn't have any more issues.

Page 1 of 2 12 LastLast

Similar Threads

  1. Adding modify data to dxf code
    By mvsawyer in forum AutoLISP
    Replies: 13
    Last Post: 2015-07-17, 06:46 PM
  2. Adding code generated entities to a selection set
    By lambwill in forum Dot Net API
    Replies: 3
    Last Post: 2010-08-19, 05:51 AM
  3. Courtesy, adding comments to code
    By artisteroi in forum VBA/COM Interop
    Replies: 8
    Last Post: 2007-05-25, 01:56 AM
  4. Replies: 7
    Last Post: 2007-01-26, 03:02 PM

Tags for this Thread

Posting Permissions

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