Jump to content

Recommended Posts

Posted

Hi,

 

Is there a way to count a number of files in a folder using an autolisp coding.

 

Thx!

  • Replies 47
  • Created
  • Last Reply

Top Posters In This Topic

  • CadFrank

    23

  • Tharwat

    12

  • BlackBox

    5

  • hanhphuc

    4

Posted

I'm assuming there's a reason you want something other than just right clicking on the folder and selecting properties? :)

Posted

Just an example ;)

 

(if (setq f (vl-directory-files "C:\\Users\\Tharwat\\Desktop\\New Folder" nil 1))
 (length f)
 )

Posted
(vl-load-com)

(defun c:FOO (/ *error* oShell oFolder path i)

 (defun *error* (msg)
   (if oShell
     (vlax-release-object oShell)
   )
   (cond ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
   )
   (princ)
 )

 (if
   (and
     (setq oShell (vla-getinterfaceobject
                    (vlax-get-acad-object)
                    "Shell.Application"
                  )
     )
     (setq oFolder (vlax-invoke
                     oShell
                     'BrowseForFolder
                     (vla-get-hwnd acApp)
                     "Select folder to search:"
                     0
                     (+ 1 64 256)
                   )
     )
     (setq path (vlax-get-property
                  (vlax-get-property oFolder 'Self)
                  'Path
                )
     )
   )
    (progn
      (prompt (strcat "\nSelected folder: \"" path "\" "))
      (prompt
        (strcat
          "\n"
          (itoa (setq i (length (vl-directory-files path "*.*" 1))))
          (if (= 1 i)
            " file "
            " files "
          )
          "found."
        )
      )
    )
 )
 (*error* nil)
)

Posted

Well reason would be I'm trying to input a value in an attribut according to de number of drawing in the file.

Posted

Well thank Tharwat it almost what I need ill figure out the rest.

 

I changed

 (if (setq f (vl-directory-files (getvar 'dwgprefix) nil 1))
 (length f)
 )

 

Thing is since the file is opened it count the .dwl and .dwl2

 

But like it said ill figure it out

 

Cheers & Beer !

Posted
Well thank Tharwat it almost what I need ill figure out the rest.

 

I changed

 (if (setq f (vl-directory-files (getvar 'dwgprefix) nil 1))
 (length f)
 )

 

Thing is since the file is opened it count the .dwl and .dwl2

 

But like it said ill figure it out

 

Cheers & Beer !

 

You have to set the Extension parameter of the vl-Directory-Files function accordingly... "*.dwg" as example.

Posted
Well thank Tharwat it almost what I need ill figure out the rest.

 

I changed

 (if (setq f (vl-directory-files (getvar 'dwgprefix) nil 1))
 (length f)
 )

 

Thing is since the file is opened it count the .dwl and .dwl2

 

But like it said ill figure it out

 

Cheers & Beer !

 

have a play with this then :)

 

(if
 (and (setq
        f (vl-directory-files
            (getvar 'dwgprefix)
            nil
            1
          )
      )
      (setq l (vl-remove-if '(lambda (ex) (wcmatch ex "*.dwl,*.dwl2")) f))
 )
  (length l)
)

Posted
have a play with this then :)

 

(if
 (and (setq
        f (vl-directory-files
            (getvar 'dwgprefix)
            nil
            1
          )
      )
      (setq l (vl-remove-if '(lambda (ex) (wcmatch ex "*.dwl,*.dwl2")) f))
 )
  (length l)
)

 

Is the OP attempting to quantify everything except *.dwl* files, or all files of one or more specific extensions?

Posted

I am trying to get the .dwg files.

 

But I wanted to figured out the rest.

 

:D

 

Thx !

Posted
Is the OP attempting to quantify everything except *.dwl* files, or all files of one or more specific extensions?

 

I guess all files as it is clear from the title of the thread :)

Posted

But I wanted to figured out the rest.

 

I can certainly appreciate that, however....

 

I am trying to get the .dwg files.

 

... ^^ This ^^ should have been included here, in order to avoid this:

 

 

 

I guess all files as it is clear from the title of the thread :)

 

 

 

 

Cheers

Posted
I am trying to get the .dwg files.

 

But I wanted to figured out the rest.

 

:D

 

Thx !

 

So give your self a favor by reading about the function vl-directory-files

Posted

Well I was going to !

 

I ain't an guru yet :P

 

Thx for the help !

Posted
Well I was going to !

 

I ain't an guru yet :P

 

Thx for the help !

 

Not a problem , just ask if you stuck with anything :)

Posted

Well I just read on the Vl-derectory-files. Now I feel bad it actually says what I wanted lol !

 

Shoulda dug more hehe would've found the answer in my book :)

Posted
(if (setq f (vl-directory-files "C:\\Users\\Tharwat\\Desktop\\New Folder" nil 1))
 (length f)
 )

 

FWIW, the if statement is not required since (length nil) = 0

 

(length (vl-directory-files (getvar 'dwgprefix) "*.dwg" 1))

Posted
FWIW, the if statement is not required since (length nil) = 0

 

 

I am always concern about the first statement before feeding or processing to conclusion and good to know that length function would exit safely if argument is equal to nil . :)

 

Thanks a lot .

Posted

Ok! well i've been on this for a few more hours and can't figure it out!

 

Now If I want to make sure that in the file it counts files with a specific string only.

 

For exemple :

 

(setq
        f (vl-directory-files
            (getvar 'dwgprefix)
            "*.dwg"
            1
          )
      )

 

("1456546756.dwg" "2014-08-11 - LU-14094 - Plan de coffrage des culées - rev.00.dwg" "2014-08-11 - LU-14094-02 - Plan de coffrage des culées - rev.00.dwg")

 

Now using :

 

(setq l (vl-remove-if '(lambda (ex) (wcmatch ex "*LU-14094*")) f))

 

("1456546756.dwg")

 

Now I've tried alot and can't seem to figure out how to extract the other one.

 

Any one can Help plz!

Posted

maybe?

(setq l (vl-remove-if[color="red"]-not[/color] '(lambda (ex) (wcmatch ex "*LU-14094*")) f))

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