Copy multiple files from a folder and paste them to another folder depending on the names of the images

25 views Asked by At

I have a folder called ntl which contains the following subfolders: beijing, cairo, delhi, la, mexico, rome, sydney. For example the path of the folders is C:\Users\nikos\OneDrive\Desktop\ntl\beijing, or C:\Users\nikos\OneDrive\Desktop\ntl\mexico and so on. Inside each subfolder there are many tif images named for example, t2013_01.tif, t2018_07.tif, t2023_12.tif etc. The name of the tif images means (for example the image t2023_12):

  1. t = time
  2. the next 4 strings are the year (2023)
  3. the 2 strings after the underscore is the month (12).

On a separate folder (outside the ntl folder) I have a folder called tst. Inside that folder I have the following subfolders: beijing, cairo, delhi, la, mexico, rome, sydney. For example the path of the folders is C:\Users\nikos\OneDrive\Desktop\tst\beijing, or C:\Users\nikos\OneDrive\Desktop\tst\delhi and so on. Inside each subfolder there are subsubfolders with the following names: 2013, 2014, 2015, ...., 2023. For example a path to these subsubfolders is: C:\Users\nikos\OneDrive\Desktop\tst\beijing\2013 or C:\Users\nikos\OneDrive\Desktop\tst\la\2022 and so on. Inside each subsubfolder there are 12 subfolders named: 01, 02, 03, ..., 12. For example a path is: C:\Users\nikos\OneDrive\Desktop\tst\beijing\2013\01, or C:\Users\nikos\OneDrive\Desktop\tst\beijing\2013\02, or C:\Users\nikos\OneDrive\Desktop\tst\beijing\2013\11 and so on.

My goal is to copy the tif images from the ntl/city folder and paste them to the tst/city/year/month folder depending on the date and time written in the name of the tif image. For example if I copy an image from the path C:\Users\nikos\OneDrive\Desktop\ntl\beijing\t2013_07.tif, this image should be pasted to the C:\Users\nikos\OneDrive\Desktop\tst\beijing\2013\07\t2017_07.tif path. Another example, if I copy an image from the path C:\Users\nikos\OneDrive\Desktop\ntl\rome\t2019_08.tif, this image should be pasted to the C:\Users\nikos\OneDrive\Desktop\tst\rome\2019\08\t2019_08.tif path.

More over, when I paste an image, I want to rename it to ntl.tif.

How can I do this in R? I don't a code to share as my attempts were all wrong.

R 4.3.2, RStudio 2023.12.1 Build 402, Windows 11.

1

There are 1 answers

0
nowhereman On

My thought process regarding this problem:

  1. Create a character vector with the city names
  2. Go through each city name with a for-loop.
  3. Combine the city name with the \ntl directory using the file.path() function. e.g. city_dir <- file.path("C:\Users\nikos\OneDrive\Desktop\ntl", city_name)
  4. Get files in each city directory with the city_files <- list.files(city_dir)
  5. Check every city_files based on its separator value. Using strsplit(city_files[i], split = '_') could work in a separate for-loop.
  6. Copy each file to the 'tst' directory based on your conditions.

Each of these steps are relatively simple and you can figure it out with a simple google query. Good luck!