Clojure - Is it possible to increment a variable within a doseq statement?

clojure

Solution

use `file-seq` and `map-indexed`:

(require '[clojure.java.io :as io])

(dorun
  (->>
    (file-seq (io/file "/home/eduard/Downloads"))
    (filter #(re-find #".+\.pdf$" (.getName %)))
    (map-indexed (fn [i v] [i v]))))

Change function in `map-indexed` to rename and you're done. The sample output for pdf files:

([0 #<File /home/eduard/Downloads/some.pdf>] ...)

Problem

I am trying to iterate over a list of files in a given directory, and add an incrementing variable i = {1,2,3.....} to their names. Here is the code I have for iterating through the files and changing each file's name: ``` (defn addCounterToExtIn [d] (def i 0) (doseq [f (.listFiles (file d)) ] ; make a sequence of all files in d (if (and (not (.isDirectory f)) ; if file is not a directry and (= '(\. \i \n) (take-last 3 (.getName f))) ) ; if it ends with .in (fs/rename f (str d '/ i (.getName f)))))) ; add i to start of its name ``` I don't know how can I increment `i` as `doseq` iterates through each file. Alternatively, is there a better loop to use to achieve the desired result?

Original source