1

I have 25 text files in my folder to be tested. But I don't want to write code for 25 files which means that it is 25 rows to test those files.

(myfunc "myfiles/txtfile1.txt")
(myfunc "myfiles/txtfile2.txt")
...

How can I use a for loop to invoke this function? Like the example code below. Sorry for my poor English

(loop for x from 1 to 25
  do ((myfunc "myfiles/txtfile~a.txt" x))
) 
)
2
  • ` do ((myfunc "myfiles/txtfile~a.txt" x))` beware, the two following parenthesis are wrong. Commented May 7, 2020 at 9:58
  • Here's a primer on format directives: lispcookbook.github.io/cl-cookbook/… Commented May 7, 2020 at 9:59

3 Answers 3

2

A simple way is to use the format function (manual):

(loop for x from 1 to 25
      do (myfunc (format nil "myfiles/txtfile~a.txt" x)))

Since all the files are in a directory, you could also give a look to the directory predefined function (manual, question in SO).

Sign up to request clarification or add additional context in comments.

Comments

0

Some functional approach

(mapcar function (directory (merge-pathnames wildcard path)))

mapcar - applies function to the provided list of arguments generated by directory. function has to accept only one argument.

Working Example

61650326.lisp

;;;; File     : 61650326.lisp
;;;; Modified : <2020-5-08 Fri 08:51:12 BST>
;;;; URL      : https://stackoverflow.com/questions/61650326

(defparameter *user-path* "t/user/myfiles/")
(defparameter *wildcard* "*.txt")
(defparameter *function* 'type-of)

(defun map-on-files (path wildcard function)
    "Map FUNCTION on a list of files found under PATH WILDCARD."
    (mapcar function (directory (merge-pathnames wildcard path))))

(format t "~{~A~%~}" (map-on-files *user-path* *wildcard* *function*))

Generating some files for test purpose:

~$: mkdir -p t/user/myfiles
~$: for i in $(seq 10); do mktemp -p t/user/myfiles/ --suffix=.txt; done
t/user/myfiles/tmp.S4wKkJ85sq.txt
t/user/myfiles/tmp.j3XeT2hneq.txt
t/user/myfiles/tmp.mut6GLhlaT.txt
t/user/myfiles/tmp.1l0I5oGDaR.txt
t/user/myfiles/tmp.hBpvOfarye.txt
t/user/myfiles/tmp.m1WfmZxrU8.txt
t/user/myfiles/tmp.o6QbvjXMh9.txt
t/user/myfiles/tmp.6CmPvWf7GO.txt
t/user/myfiles/tmp.ZNWcaymY0g.txt
t/user/myfiles/tmp.JfUQ4cW0dD.txt

And Run code snippet

~$: sbcl --script 61650326.lisp
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME
PATHNAME

3 Comments

Actually, that's exactly what I want to do but I am new on Lisp. My path is "user/myfiles/textfile.txt" can you give me an example from this path how I can read all files in this folder?
@JuanRodre I've extended answer with more explicit example
Thank you so much @Hellseher
0

You can concatenate the digit at the end of the filename with format:

(loop for x from 1 to 25 do (myfunc (format nil "myfiles/txtfile~a.txt" x)) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.