Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List files that end with pattern and lack an extension

I have a directory with multiple subdirectories that contain files.
The files themselves have no extension; however, each file has an additional header file with the extension ".hdr".

In R, I want to list all file names that contain the string map_masked and end with the pattern "masked", but I only want the files without an extension (the ones that end with the pattern, not the header files).

As suggested in this answer, I tried to use the $ sign to indicate the pattern should occur at the end of a line.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code I used:

dir <- "/my/directory"

list.files(dir, pattern = "map_masked|masked$", recursive = TRUE)

The output, however, looks as follows:

[1] "subdirectory/something_map_masked_something_masked"
[2] "subdirectory/something_map_masked_something_masked.hdr"
etc.

Now, how do I tell R to exclude the files that have an ".hdr" extension?
I am aware this could easily be done by applying a filter on the output, but I would rather like to know what is wrong with my code and understand why R behaves the way it does in this case.

>Solution :

You can use

list.files(dir, pattern = "map_masked.*masked$", recursive = TRUE)

It returns filepaths that contain map_masked and end with masked string.

Details:

  • map_masked – a fixed string
  • .* – any zero or more chars as many as possible
  • masked – a masked substring
  • $ – end of string.

See the regex demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading