Newlisp recursive access to the directory tree

Source: Internet
Author: User

A simple recursive function can traverse all the files in the directory tree and pass another processing function. This function can take the file path as a parameter to determine how to handle it.

The following is a call example:


#!/usr/bin/newlisp                                                                       (load "file.lsp")(define (show-file file-path)  (println (string file-path ": " (file-info file-path)))           )(FILE:recursive-access-dir "/opt/" show-file)(exit)

Recursive-access-DIR is a recursive function. Show-file is another function that displays the received file path and file-Info information.

(context ‘FILE)(define (recursive-access-dir dir-path file-op)  (dolist (nde (directory dir-path {^[^.]}))    (if (directory? (append dir-path nde))        (recursive-access-dir (append dir-path nde "/") file-op)       (file-op (append dir-path nde)))))


Note:

The regular expression ^ [^.] indicates that only files or directories not starting with "." are searched, so "." and "." are excluded ..

This recursive algorithm is a depth-first algorithm. Once a directory is found, all the paths under it will be traversed before returning to the beginning, and then processing the next sibling directory.



Newlisp recursive access to the directory tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.