Check the existence of all files in the list

Check the existence of all files in the list

Browning_Zed
Advocate Advocate
681 Views
2 Replies
Message 1 of 3

Check the existence of all files in the list

Browning_Zed
Advocate
Advocate

Hi everyone.

I need to make a condition to check for the existence of files, something like this:

 

    (if (and 
            (findfile "D:\\File1.txt")
            (findfile "D:\\File2.txt")
            (findfile "D:\\File3.txt")
        )
        (progn ...)
    )

 

 The problem is that the names of all the files are included in the list

 

   '("File1" "File2" "File3")

 

thus, it is necessary to check the existence of all files in the list. I tried to do the following:

 

    (if (foreach f 
           '("File1" "File2" "File3")
            (findfile (strcat "D:\\" f ".txt"))
        )
        (progn ...)
    )

 

but this always returns true whether the files exist or not. How can I check the existence of files in this case?

0 Likes
Accepted solutions (2)
682 Views
2 Replies
Replies (2)
Message 2 of 3

hak_vz
Advisor
Advisor
Accepted solution

 

(foreach f '("File1" "File2" "File3")(setq chk (cons (findfile (strcat "D:\\" f ".txt")) chk)))
(if (apply 'and chk))
(progn ...)
)​

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 3

Kent1Cooper
Consultant
Consultant
Accepted solution

Another way:

 

(setq lst '("File1" "File2" "File3"))

(apply 'and (mapcar '(lambda (x) (findfile (strcat "D:/" x ".txt"))) lst))

 

Returns T if they all exist, nil otherwise.

Kent Cooper, AIA
0 Likes