'Select xml files and their corresponding jpg files from a folder with R

I have have a folder with about 4000 jpg files and only about 50 of them have a corresponding xml file. I want to select those jpg files that have a corresponding xml file and put them into a separate folder using R.

r


Solution 1:[1]

No worries. Suppose you want to copy the corresponding ".jpg" files from your current working directory to a subdirectory called "tmp". This does the trick:

library(magrittr)
list.files() %>% split(., grepl("\\.xml$", .)) %>%
      { gsub("\\.xml$", ".jpg", .[[2]]) } %T>%
      { if (!isTRUE(file.info("tmp")$isdir)) dir.create("tmp") } %>%
      file.copy("tmp")

Package magrittr is what gives you the pipe %>% and tee pipe %T>% operators that allow you to do all this in a single line of code.

The code gets a list of files in the current working directory, identifies file names ending in ".xml", creates their ".jpg" equivalents, and copies them over to a subdirectory called "tmp".

Before copying the files, the code tests whether the directory that is to receive the files exists, and creates it if it does not. The tee pipe %T>% is used to move the data about file names to the next step where they get copied into that directory.

Of course, the code presumes that you don't have unpartnered ".xml" files, and that the file names match up except by their final extension. It will throw a warning message if some ".xml" files don't have matching ".jpg" files, but it will still copy the ones that do.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1