rename multiple files and folders

Hi I'm trying to rename multiple files and folder that are in the following format:
there are multiple files with the same format in the following folders as welll

Chan12_Sitting height ratio || id-1071_CAD
DIAGRAM_Type 2 diabetes || id-976_CAD

to
Chan12_id-1071; and
DIAGRAM_id-976

I'm not sure if I understand you correctly but if those are file names, you could do something like this

original_names <- list.files(pattern="_CAD")
new_names <- paste0(str_extract(original_names, "^.+?_"),
                    str_extract(original_names, "id.+(?=_CAD)"))
file.rename(original_names, new_names)
1 Like

Thanks! it worked perfectly

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.