Author Topic: Test if item is a file or folder  (Read 2926 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Test if item is a file or folder
« on: July 07, 2010, 11:35:57 AM »
I'm currently using vl-file-systime, since it will return nil if attempted on a folder. Does anyone have a better method?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Test if item is a file or folder
« Reply #1 on: July 07, 2010, 11:39:49 AM »
vl-file-directory-p
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Test if item is a file or folder
« Reply #2 on: July 07, 2010, 11:42:56 AM »
Well, I just feel like a dumb ass. Thanks Michael.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Test if item is a file or folder
« Reply #3 on: July 07, 2010, 11:48:56 AM »
Well, I just feel like a dumb ass.

Worry not -- so you don't know everything -- you're just like the rest of us.

And you're very welcome.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Test if item is a file or folder
« Reply #4 on: July 07, 2010, 11:52:55 AM »
Some cat skinning  8-)

Code: [Select]
(defun LM:isFile ( filename / fso result )
  ;; Lee Mac 2010
  (vl-load-com)

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq result (not (zerop (vlax-invoke fso 'FileExists filename))))
  (vlax-release-object fso)

  result
)

(defun LM:isDirectory ( directory / fso result )
  ;; Lee Mac 2010
  (vl-load-com)

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq result (not (zerop (vlax-invoke fso 'FolderExists directory))))
  (vlax-release-object fso)

  result
)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Test if item is a file or folder
« Reply #5 on: July 07, 2010, 11:56:26 AM »
Well, I just feel like a dumb ass.

Worry not -- so you don't know everything -- you're just like the rest of us.

And you're very welcome.
:-)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Test if item is a file or folder
« Reply #6 on: July 07, 2010, 02:10:20 PM »
Calling (vl-file-directory-p...) several times in a loop can cause noticeable delays, so I try to organize the same-folder searches with a single call to (vl-directory-files...) with arguments to return folders only, then check for membership in the returned list.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Test if item is a file or folder
« Reply #7 on: July 07, 2010, 02:26:49 PM »
Calling (vl-file-directory-p...) several times in a loop can cause noticeable delays, so I try to organize the same-folder searches with a single call to (vl-directory-files...) with arguments to return folders only, then check for membership in the returned list.
Good to know. However, in my situation, I was creating a list of all files existing in a specified directory, including any files located in subsequent directories. So I had to be able to check if an item in the list was a file or directory.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Test if item is a file or folder
« Reply #8 on: July 07, 2010, 05:17:38 PM »
one must remember that (vl-file-directory-p ...) will return nil if directory name ends with "." :(

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Test if item is a file or folder
« Reply #9 on: July 07, 2010, 06:08:51 PM »
one must remember that (vl-file-directory-p ...) will return nil if directory name ends with "." :(
Yeah, I took that into account. The really annoying part is, you can't just (cddr (vl-file-directory, since if there isn't a parent to the specified directory, it will not return the "." and ".." with the file list.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Test if item is a file or folder
« Reply #10 on: July 08, 2010, 02:54:24 AM »
there is a certain feature/bug in vl-file... functions, so this should help
Code: [Select]
(defun vk_StringSubstPat
(NewPattern Pattern String Pos / NewPatternLen)
  (setq NewPatternLen (strlen NewPattern))
  (while (setq Pos (vl-string-search Pattern String Pos))
    (setq String (vl-string-subst NewPattern Pattern String Pos))
    (setq Pos (+ Pos NewPatternLen))
  )
  String
)
(defun vk_Fool_vl-file_function (n)
  (vk_StringSubstPat ".\\" "\\" (strcat (vl-string-right-trim "\\" n) "\\") 3)
)