Author Topic: rename files in a folder  (Read 5010 times)

0 Members and 1 Guest are viewing this topic.

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
rename files in a folder
« on: April 09, 2013, 04:43:28 AM »
Hi !

I try to rename filenames in a dictionary, but something I do wrong. I can´t debugg what is in forreach N. It´s empty - think I to easy.

Code: [Select]
(defun renameFile ()
  (foreach X (vl-directory-files "c:/pdfplot" "*.pdf")
    (foreach Y (layoutlist)
      (vl-file-rename X (strcat Y ".pdf"))
      )
    )
  )


« Last Edit: April 09, 2013, 04:57:43 AM by cadplayer »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: rename files in a folder
« Reply #1 on: April 09, 2013, 08:26:54 AM »
Here is a break-down of what your function is currently doing:

Code: [Select]
(defun renamefile ( )

    (foreach x (vl-directory-files "c:/pdfplot" "*.pdf")

        ;; Assuming the directory 'c:/pdfplot' exists
        ;; and contains one or more files with the .pdf
        ;; extension, since vl-directory-files has been
        ;; called without the 'directories' integer argument
        ;; the foreach symbol 'x' could be a PDF filename,
        ;; the name of a subfolder within the directory 'c:/pdfplot'
        ;; or the current folder & parent folder placeholders "." & ".."

        (foreach y (layoutlist)

            ;; The foreach symbol 'y' will now be assigned the name
            ;; of each paperspace drawing layout in the active drawing

            (vl-file-rename x (strcat y ".pdf"))

            ;; Below is what the above expression may look like within
            ;; this foreach loop:
            ;;
            ;; (vl-file-rename "myfile.pdf" "Layout1.pdf")
            ;; (vl-file-rename "myfile.pdf" "Layout2.pdf")
            ;; ...
            ;; (vl-file-rename "myfile.pdf" "Layout3.pdf")
            ;;
            ;; Note that 'x' only holds the filename of the PDF file,
            ;; not the full filepath, hence AutoCAD will search the
            ;; working directory for a file with filename equal to
            ;; that held by 'x'.
            ;;
            ;; If such a file can be found, this file is renamed to
            ;; the first layout in the list returned by (layoutlist)
            ;;
            ;; If this renaming operation is successful, the next
            ;; evaluation of the vl-file-rename function will fail
            ;; since the filename held by the symbol 'x' no longer
            ;; exists.
            ;;
            ;; If the rename operation has failed because the file
            ;; held by the symbol 'x' cannot be found, then all
            ;; subsequent calls to the vl-file-rename function will
            ;; also fail within the nested foreach loop, when the
            ;; function attempts to rename the file to every layout
            ;; name in the active drawing.

        )
    )
)

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: rename files in a folder
« Reply #2 on: April 10, 2013, 02:42:01 AM »
Thanks Lee!

In my example it works but not perfect, two problems I have. When in folder c:\pdfplot are old files, the renaming process could be wrong and in second part of code 
if I only debugg the code works but not in programprocess, the last file is not renamed.
Code: [Select]
(defun c:PDF ( / )
  ;; first part
  ;; Print layouts as pdf-file
  ;; you have to have searchpath c:\pdfplot there pdf-files be saved
  (foreach layoutname (layoutlist)
    (command "._layout" "set" layoutname)
    (command "_plot" "_y" layoutname "PDFcreator.pc3" "A1" "Millimeters" "Landscape" "No"
     "_e" "1000=1" "_c" "_y" "A1.ctb" "_y" "_n" "_n" "_n" "_n" "_n" "_y" )
    )
  (princ "\nComplete...")
  (princ)
  ;; second part
  ;; Rename pdf-files by layoutnames which is in folder c:\pdfplot
  (foreach x (vl-directory-files "c:/pdfplot" "*.pdf")
    (foreach y (layoutlist)
      (vl-file-rename (strcat "c:/pdfplot/" x) (strcat "c:/pdfplot/" y ".pdf"))
      )
    )
  (princ (strcat "\n" (itoa (length (vl-directory-files "c:/pdfplot" "*.pdf"))) "\nFiles renamed "))
  (princ)
  )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: rename files in a folder
« Reply #3 on: April 10, 2013, 08:00:03 AM »
Try something like this Dirk:
Code: [Select]
(defun c:PDF ( / dir exs files layouts )

    (setq dir "C:\\pdfplot"
          exs (vl-directory-files dir "*.pdf" 1) ;; Existing PDF files in Directory
    )     
    (foreach layout (setq layouts (layoutlist))
        (setvar 'ctab layout)
        (command
            "_.-plot"
            "_Y"   ;; Detailed plot configuration? [Yes/No]:
            layout ;; Enter a layout name <Current-Layout>:
            "PDFcreator.pc3" ;; Enter an output device name:
            "A1" ;; Enter paper size:
            "_M" ;; Enter paper units [Inches/Millimeters]:
            "_L" ;; Enter drawing orientation [Portrait/Landscape]:
            "_N" ;; Plot upside down? [Yes/No]:
            "_E" ;; Enter plot area [Display/Extents/Limits/View/Window]:
            "1000=1" ;; Enter plot scale (Plotted Inches=Drawing Units) or [Fit] <1=1>:
            "_C" ;; Enter plot offset (x,y) or [Center]:
            "_Y" ;; Plot with plot styles? [Yes/No]:
            "A1.ctb" ;; Enter plot style table name (enter . for none):
            "_Y" ;; Plot with lineweights? [Yes/No]:
            "_N" ;; Scale lineweights with plot scale? [Yes/No]:
            "_N" ;; Plot paper space first? [Yes/No]:
            "_N" ;; Hide paperspace objects? [Yes/No]:
            "_N" ;; Write the plot to a file [Yes/No] <N>:
            "_N" ;; Save changes to page setup [Yes/No]:
            "_Y" ;; Proceed with plot [Yes/No] <Y>:
        )
    )
    (if
        (setq files
            (mapcar '(lambda ( x ) (strcat dir "\\" x))
                (vl-remove-if (function (lambda ( x ) (member x exs)))
                    (vl-directory-files dir "*.pdf" 1)
                )
            )
        )
        (mapcar
            (function
                (lambda ( n layout )
                    (vl-file-rename (nth n files) (strcat dir "\\" layout ".pdf"))
                )
            )
            (vl-sort-i (mapcar 'vl-file-systime files)
                (function
                    (lambda ( a b )
                        (while (and (car a) (car b) (= (car a) (car b)))
                            (setq a (cdr a)
                                  b (cdr b)
                            )
                        )
                        (< (car a) (car b))
                    )
                )
            )
            layouts
        )
    )
    (princ)
)

The above will remove files present in the list returned by the vl-directory-files function which were already present in the plot directory before the plot operation, ensuring that only the plotted files are renamed (assuming no other PDF files are created in the directory during plotting).

The program then sorts the filenames by the order in which they were created, using the return of the vl-file-systime function; this ensures that the files are assigned the correct layout name, removing the assumption that the list of filenames returned by vl-directory-files will be sorted by the order in which the files were created.

Finally, I have included the prompts for the PLOT command to make modification easier for others who wish to use this program.

The above program is untested however.

Patrick_35

  • Guest
Re: rename files in a folder
« Reply #4 on: April 11, 2013, 11:27:10 AM »

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: rename files in a folder
« Reply #5 on: April 12, 2013, 02:02:52 AM »
@Lee
Very nice, do you understand why not renamed last pdf-filename ?

@Patrick
Can you explain, what your code do exactly.

« Last Edit: April 12, 2013, 02:06:21 AM by cadplayer »

Patrick_35

  • Guest
Re: rename files in a folder
« Reply #6 on: April 12, 2013, 02:17:58 AM »
Instead of renaming files is to use pdfcreator directly and indicate which file to write

An example who print test page in "c:\temp\test.pdf"

Code: [Select]
(defun c:test(/ kill dir file pdf)
  (defun kill(qui / item meth1 meth2 obj WMI)
    (setq WMI (vlax-create-object "WbemScripting.SWbemLocator")
          meth1 (vlax-invoke WMI 'ConnectServer nil nil nil nil nil nil nil nil)
          meth2 (vlax-invoke meth1 'ExecQuery "Select * from Win32_Process")
    )
    (vlax-for item meth2
      (and (setq obj (vlax-get item 'CommandLine)) (vl-string-search (strcase qui) (strcase obj))
        (vlax-invoke item 'Terminate)
      )
    )
    (foreach item (list WMI meth1 meth2)
      (vlax-release-object item)
    )
  )

  ; Directory
  (setq dir "c:\\temp")
  ; File
  (setq file "test")

  ; Eradicate PdfCreator if any
  (kill "PDFCreator.exe")

  ; Create an instance with PdfCreator
  (setq pdf (vlax-create-object "PDFCreator.clsPDFCreator"))

  ; Parameters PdfCreator
  (vlax-invoke pdf 'cstart "/NoProcessingAtStartup")
  (vlax-put pdf 'cPrinterStop :vlax-true)
  (mapcar '(lambda(a b)
    (vlax-put-property pdf 'cOption a b)
  )
  (list "UseAutosave" "UseAutosaveDirectory" "AutosaveDirectory" "AutosaveFilename" "AutosaveFormat")
  (list 1 1 dir file 0)
  )

  ; Clear the cache
  (vlax-invoke pdf 'cClearCache)

  ; View PdfCreator
  (vlax-put pdf 'cWindowState 0)

  ; Print test page
  (vlax-invoke pdf 'cPrintPDFCreatorTestpage)

  ; Pause
  (while (/= (vlax-get pdf 'cCountOfPrintjobs) 1))

  ; Save Pdf
  (vlax-put pdf 'cPrinterStop 0)

  ; Clear
  (vlax-release-object pdf)

  ; Silence
  (princ)
)

@+
« Last Edit: April 12, 2013, 05:16:11 AM by Patrick_35 »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: rename files in a folder
« Reply #7 on: April 12, 2013, 07:27:48 AM »
@Lee
Very nice, do you understand why not renamed last pdf-filename ?

I've not tested the program, however, looking back over my code, I see no reason why the program should not rename the last PDF file generated - perhaps others on the forum can spot something that I have overlooked in my code. :-\

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: rename files in a folder
« Reply #8 on: April 16, 2013, 03:47:02 AM »
@ Lee Mac:
Maybe inserting a delay after the (foreach) loop is required.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: rename files in a folder
« Reply #9 on: April 16, 2013, 06:19:51 AM »
@ Lee Mac:
Maybe inserting a delay after the (foreach) loop is required.

Good call roy.

Try this Dirk:
Code: [Select]
(defun c:PDF ( / dir exs files layouts )

    (setq dir "C:\\pdfplot"
          exs (vl-directory-files dir "*.pdf" 1) ;; Existing PDF files in Directory
    )     
    (foreach layout (setq layouts (layoutlist))
        (setvar 'ctab layout)
        (command
            "_.-plot"
            "_Y"   ;; Detailed plot configuration? [Yes/No]:
            layout ;; Enter a layout name <Current-Layout>:
            "PDFcreator.pc3" ;; Enter an output device name:
            "A1" ;; Enter paper size:
            "_M" ;; Enter paper units [Inches/Millimeters]:
            "_L" ;; Enter drawing orientation [Portrait/Landscape]:
            "_N" ;; Plot upside down? [Yes/No]:
            "_E" ;; Enter plot area [Display/Extents/Limits/View/Window]:
            "1000=1" ;; Enter plot scale (Plotted Inches=Drawing Units) or [Fit] <1=1>:
            "_C" ;; Enter plot offset (x,y) or [Center]:
            "_Y" ;; Plot with plot styles? [Yes/No]:
            "A1.ctb" ;; Enter plot style table name (enter . for none):
            "_Y" ;; Plot with lineweights? [Yes/No]:
            "_N" ;; Scale lineweights with plot scale? [Yes/No]:
            "_N" ;; Plot paper space first? [Yes/No]:
            "_N" ;; Hide paperspace objects? [Yes/No]:
            "_N" ;; Write the plot to a file [Yes/No] <N>:
            "_N" ;; Save changes to page setup [Yes/No]:
            "_Y" ;; Proceed with plot [Yes/No] <Y>:
        )
    )

    (command "_.delay" 1000) ;; Alter this as required
   
    (if
        (setq files
            (mapcar '(lambda ( x ) (strcat dir "\\" x))
                (vl-remove-if (function (lambda ( x ) (member x exs)))
                    (vl-directory-files dir "*.pdf" 1)
                )
            )
        )
        (mapcar
            (function
                (lambda ( n layout )
                    (vl-file-rename (nth n files) (strcat dir "\\" layout ".pdf"))
                )
            )
            (vl-sort-i (mapcar 'vl-file-systime files)
                (function
                    (lambda ( a b )
                        (while (and (car a) (car b) (= (car a) (car b)))
                            (setq a (cdr a)
                                  b (cdr b)
                            )
                        )
                        (< (car a) (car b))
                    )
                )
            )
            layouts
        )
    )
    (princ)
)

If the file is still not renamed, try increasing the delay.

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: rename files in a folder
« Reply #10 on: April 17, 2013, 09:47:35 AM »
Very fine, you´re the best.
It works perfect.
Thanx

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: rename files in a folder
« Reply #11 on: April 17, 2013, 09:53:28 AM »
Excellent to hear Dirk; thanks again for the suggestion roy, spot on.