Author Topic: Using FOREACH to get to a sub-directory?  (Read 996 times)

0 Members and 1 Guest are viewing this topic.

sprunkle

  • Mosquito
  • Posts: 3
Using FOREACH to get to a sub-directory?
« on: May 24, 2023, 03:48:49 PM »
I am trying to use foreach for the first time, and I am hoping some of you can help me shed some light on where I am going wrong.

Code - Auto/Visual Lisp: [Select]
  1. (setq JobNum (getstring "\nEnter Job Number: "))
  2. (setq jdb
  3.     (list "J:/x000" "J:/x100" "J:/x200" "J:/x300" "J:/x400" "J:/x500" "J:/x600" "J:/x700"
  4.     "J:/x800" "J:/x900")
  5. )
  6. (foreach dir jdb (setq ofile (open (strcat dir "/" JobNum "/RoofDes.out") "r")))
  7.  

When I check !ofile after this it is nil. Is foreach stopping at the first step if it can't find the file there?

What I am trying to accomplish is to ask for user input for a job number (23-4827 for example), and then have a file open that I will pull data from. The job could be in one of the ten sub-directories listed above. It is actually the 5th character in the job number that dictates which sub-directory will be pulled from, so maybe I should use that to help open the file?

Thanks in advance for any input you may have. I am very new to AutoLISP.

ribarm

  • Gator
  • Posts: 3270
  • Marko Ribar, architect
Re: Using FOREACH to get to a sub-directory?
« Reply #1 on: May 24, 2023, 04:47:31 PM »
I think you don't need foreach at all...
Try something like this :

Code - Auto/Visual Lisp: [Select]
  1. (setq JobNum (getstring "\nEnter Job Number: "))
  2. (setq dir (car (vl-remove-if '(lambda (x) (/= (substr x 4 1) (substr JobNum 4 1))) (list "J:/x000" "J:/x100" "J:/x200" "J:/x300" "J:/x400" "J:/x500" "J:/x600" "J:/x700" "J:/x800" "J:/x900"))))
  3. (setq ofile (open (strcat dir "/" JobNum "/RoofDes.out") "r")
  4.  

Still, not thoroughly tested and debugged, but should give you some shed of light IMHO...
« Last Edit: May 24, 2023, 04:51:05 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

sprunkle

  • Mosquito
  • Posts: 3
Re: Using FOREACH to get to a sub-directory?
« Reply #2 on: May 24, 2023, 05:33:56 PM »
Ribarm, thank you! I changed a couple of things, but that works very well!

Code - Auto/Visual Lisp: [Select]
  1. (setq JobNum (getstring "\nEnter Job Number: "))
  2. (setq dir (list "J:/x000" "J:/x100" "J:/x200" "J:/x300" "J:/x400" "J:/x500" "J:/x600" "J:/x700" "J:/x800" "J:/x900"))
  3. (setq dir (car (vl-remove-if '(lambda (x) (/= (substr x 5 1) (substr JobNum 5 1))) dir)))
  4. (setq ofile (open (strcat dir "/" JobNum "/RoofDes.out") "r"))

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Using FOREACH to get to a sub-directory?
« Reply #3 on: June 06, 2023, 05:11:00 PM »
Why not simply -
Code - Auto/Visual Lisp: [Select]
  1. (setq JobNum (getstring "\nEnter Job Number: ")
  2.       ofile  (open (strcat "J:/x" (substr JobNum 5 1) "00/" JobNum "/RoofDes.out") "r")
  3. )