defun-form below defines a new interactive command that does the job.
If you need this functionality more often you can put this
defun-form into your initialization file
~/.emacs. If you need it only once you can put it into your
*scratch* buffer place point behind the last closing parenthesis and evaluate it with the key sequence C-x C-e. Note, this evaluates just the definition but not the command itself.
The first argument of
defun is the name of the newly defined command --
James-move-files in our case.
You can call this command with M-x
James-move-files RET. The
interactive form in the command definition causes that the command interactively asks you for all the needed arguments of the command.
The second argument of
James-move-files is the list of the arguments of this command.
The third argument is the comment string. This comment string is presented to you when you type C-h f
James-move-files RET. You can use the key sequence C-h f on any of the keywords in the definition of
James-move-files to learn more about its functionality.
There is more detailed help in the
elisp-info files C-h i m
elisp RET. You can search that manual with the incremental search C-s
foo. Even if tells you
Failing search: ... [end of node] press C-s again.
I hope that the help string of
James-move-files is good enough for you to use this function.
(defun James-move-files (pattern-file path-with-folders folder-name-pattern file-name-pattern target-dir)
"Copy files matching a regular expression from a series of folders from a given path. The folders are identified by IDs collected from a pattern file.
The arguments of this command are:
PATTERN-FILE: Ascii file containing IDs. Separator
may be spaces, form feeds, tabs, line feeds, carry returns, or
PATH-WITH-FOLDERS: Path where to find the folders with names marked by the IDs.
FOLDER-NAME-PATTERN: Regular expression matching a folder name with ID as first group. You can use \\([0-9]+\\) as group part of the regular expression if you want to match numbers.
FILE-NAME-PATTERN: Regular expression that matches the file to copy from the folder. Example: .*_dc_brain_FA\\.nii
TARGET-DIR: Target directory for the files to copy.
(interactive "fPattern file:\nDPath with folders:\nsFolder name pattern:\nsFile name pattern:\nDTarget directory:")
(with-current-buffer (find-file pattern-file)
(let* ((list-of-patterns (split-string (buffer-string) nil t))
(path (directory-file-name path-with-folders))) ; without trailing slash /
(mapc (lambda (folder)
(when (and (file-directory-p folder)
(string-match folder-name-pattern folder)
(member (match-string 1 folder) list-of-patterns))
(mapc (lambda (file)
(copy-file file target-dir))
(directory-files (concat path "/" folder) t file-name-pattern))))